You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何关闭SuiteScript 2.0的SFTP连接?连接未关闭致服务器连接数超限

Fixing Unclosed SFTP Connections in NetSuite SuiteScript 2.0

I’ve seen this exact connection leak issue pop up for a lot of NetSuite developers using the N/sftp module—great that you got file transfers working, but those lingering connections are a real headache. Let’s break down how to fix this properly.

The Core Problem

NetSuite’s SFTP connections don’t always auto-close reliably, especially if your script hits an error mid-transfer or runs longer than expected. Those orphaned connections stick around on your SFTP server until it hits its max connection limit, forcing that annoying restart cycle.

The Fix: Always Explicitly Close Connections in finally

The key rule here is never rely on implicit connection cleanup. You need to manually call connection.disconnect(), and wrap that call in a finally block to ensure it runs whether your transfer succeeds or fails.

Here’s how to adjust your code:

const sftp = require('N/sftp');
const file = require('N/file');

function sendFileToSFTP() {
    let sftpConnection = null;

    try {
        // Create the SFTP connection
        sftpConnection = sftp.createConnection({
            username: 'your-username',
            password: 'your-secure-password', // Consider switching to key auth for better security
            url: 'your-sftp-server-url.com',
            port: 22 // Adjust if your server uses a non-standard port
        });

        // Load your local NetSuite file
        const targetFile = file.load({ id: 12345 }); // Replace with your file ID/path

        // Upload the file
        sftpConnection.upload({
            directory: '/remote/target/directory',
            filename: targetFile.name,
            file: targetFile,
            replaceExisting: true
        });

        log.debug('Success', 'File uploaded to SFTP server');
    } catch (transferError) {
        log.error('SFTP Transfer Failed', transferError.message);
        throw transferError; // Re-throw if you want upstream processes to handle the error
    } finally {
        // Close the connection NO MATTER WHAT
        if (sftpConnection) {
            try {
                sftpConnection.disconnect();
                log.debug('Connection Closed', 'SFTP connection successfully terminated');
            } catch (disconnectError) {
                log.error('Connection Close Failed', disconnectError.message);
                // Even if closing fails, log it and move on—no need to halt here
            }
        }
    }
}

Extra Tips to Prevent Future Leaks

  • Reuse connections instead of creating new ones: If your script transfers multiple files, create one connection at the start, do all transfers, then close it once at the end. Multiple connection creations increase the risk of leaks.
  • Optimize script runtime: If your script times out, the finally block might not execute. Keep transfers efficient—batch small files, avoid unnecessary processing mid-transfer, and ensure your script stays under NetSuite’s runtime limits.
  • Set server-side idle timeouts: As a safety net, configure your SFTP server to automatically drop idle connections after a set period (e.g., 10 minutes). This will clean up any stray connections that slip through the cracks.
  • Switch to key-based authentication: Password auth works, but key-based auth is more secure and some SFTP servers handle key-authenticated connections more cleanly, reducing the chance of orphaned sessions.

Why This Happens

NetSuite’s garbage collection should theoretically clean up unused connection objects, but in practice, exceptions, timeouts, or edge cases in the N/sftp module can leave connections open on the server side. Explicitly closing in finally eliminates this uncertainty.

内容的提问来源于stack exchange,提问作者Charl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:19:28