Yaroslav Ambrozyak
Yaroslav Ambrozyak

Reputation: 1

Channel is being closed exception. Spring Boot 3.1.2 SFTP integration

we are experiencing Channel is being closed issue with Spring Boot 3 SFTP integration.

Caused by: org.apache.sshd.common.SshException: Channel is being closed
    at org.apache.sshd.sftp.client.impl.DefaultSftpClient.receive(DefaultSftpClient.java:319)
    at org.apache.sshd.sftp.client.impl.AbstractSftpClient.response(AbstractSftpClient.java:181)
    at org.apache.sshd.sftp.client.impl.AbstractSftpClient.rpc(AbstractSftpClient.java:169)
    at org.apache.sshd.sftp.client.impl.AbstractSftpClient.checkHandle(AbstractSftpClient.java:290)
    at org.apache.sshd.sftp.client.impl.AbstractSftpClient.open(AbstractSftpClient.java:589)
    at org.apache.sshd.sftp.client.impl.SftpOutputStreamAsync.<init>(SftpOutputStreamAsync.java:63)
    at org.apache.sshd.sftp.client.impl.AbstractSftpClient.write(AbstractSftpClient.java:1213)
    at org.apache.sshd.sftp.client.SftpClient.write(SftpClient.java:940)
    at org.apache.sshd.sftp.client.SftpClient.write(SftpClient.java:936)

this error happens from time to time when we execute around 50 requests to save a file to a SFTP server. The files itself is not big, up to 10Kb. To write a file we use SftpRemoteFileTemplate which is configured like this:

@Bean
public SftpRemoteFileTemplate remoteFileTemplate(SFTPConfig sftpConfig) {
    SshClient sshClient = SshClient.setUpDefaultClient();
    CoreModuleProperties.PREFERRED_AUTHS.set(sshClient, "password");
    sshClient.addPasswordIdentity(sftpConfig.password());

    DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory(sshClient, true);
    defaultSftpSessionFactory.setHost(sftpConfig.hostname());
    defaultSftpSessionFactory.setUser(sftpConfig.user());
    defaultSftpSessionFactory.setPort(sftpConfig.port());
    return new SftpRemoteFileTemplate(defaultSftpSessionFactory);
}
void write(String filePath, byte[] fileData) {
    sftpRemoteFileTemplate.execute((session) -> {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
        session.write(inputStream, filePath);
    });
}

We have tried to change pool size

SftpModuleProperties.POOL_SIZE.set(sshClient, <some number, 2, 4, 6, etc>);

and different IO service factory

MinaServiceFactoryFactory minaServiceFactoryFactory = new MinaServiceFactoryFactory();
sshClient.setIoServiceFactoryFactory(minaServiceFactoryFactory);

Also when we set the shared session flag to false we do not get Channel is being closed issue, but get Connection reset by peer because the service spawns a lot of sessions and the SFTP server starts to refuse a new one at some point of time

DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory(sshClient, false);

The same code works fine using SB 2.x under the same load. Any hints on where we should look to fix the problem?

Thanks

Upvotes: 0

Views: 348

Answers (1)

ridho rifhani
ridho rifhani

Reputation: 1

you can try this..

@Bean
public InitializingBean initializingBean(SftpRemoteFileTemplate template) {
    return () -> template
        .execute(session -> {
            try {
                session.isOpen();
            } catch (Exception e) {
                log.info("error initializingBean:{}", e);
            }
            return null;
        });
}

Upvotes: 0

Related Questions