Skip to content

SFTP: Auto-disconnect timer incorrectly closes valid connections due to inverted logic #289

@funkyshu

Description

@funkyshu

Describe the bug
The SFTP backend's auto-disconnect timer has inverted logic in connTimerStart() that prematurely closes valid, working connections after idle periods. The condition (!IsValid() || !IsNil()) closes valid clients but ignores typed-nil clients, opposite of the intended behavior.

To Reproduce
Steps to reproduce the behavior:

  1. Configure SFTP backend with AutoDisconnect enabled (default: 10 seconds)
  2. Perform an SFTP file operation (read, write, etc.)
  3. Wait for idle timeout period (10+ seconds with no operations)
  4. Attempt another SFTP operation
  5. See connection errors, hung connections, or forced reconnection delays

Expected behavior

  • Timer should close valid connections only when appropriate for auto-disconnect
  • Timer should safely handle typed-nil clients without attempting to call Close() on them
  • Subsequent operations after idle period should reconnect cleanly without errors

Actual behavior

  • Valid connections are aggressively closed by timer during idle periods
  • Users experience "connection reset", "broken pipe", or hung connection errors
  • Performance degradation from constant reconnection overhead
  • Typed-nil edge case is not handled (though this is masked by other issues)

Root Cause
Lines 143-144 and 148-149 in backend/sftp/fileSystem.go:

// BUGGY LOGIC - inverted conditions
if fs.sftpclient != nil && (!reflect.ValueOf(fs.sftpclient).IsValid() || !reflect.ValueOf(fs.sftpclient).IsNil()) {
    _ = fs.sftpclient.Close()  // Closes valid clients incorrectly
}

Should be:

// CORRECT LOGIC
if fs.sftpclient != nil && reflect.ValueOf(fs.sftpclient).IsValid() && !reflect.ValueOf(fs.sftpclient).IsNil() {
    _ = fs.sftpclient.Close()  // Only close valid, non-nil clients
}

Additional context
This bug was introduced in the recent typed-nil fix. The intention was to add typed-nil detection to prevent panics, but the boolean logic was inverted (using OR with negations instead of AND with correct conditions). The bug manifests primarily with infrequent SFTP operations where the auto-disconnect timer has time to fire between operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions