Skip to content

Avoid ChannelStdinFile GC deadlock when channel lock is held - #2687

Open
Dmao233 wants to merge 1 commit into
paramiko:mainfrom
Dmao233:cursor/channel-stdin-gc-deadlock-74fd
Open

Dmao233 wants to merge 1 commit into
paramiko:mainfrom
Dmao233:cursor/channel-stdin-gc-deadlock-74fd

Conversation

@Dmao233

@Dmao233 Dmao233 commented Sep 7, 2026

Copy link
Copy Markdown

Fixes #2685.

ChannelStdinFile inherited BufferedFile.__del__ → close() → shutdown_write(), which blocks on the channel lock. GC can run that finalizer on the Transport thread while it already holds the lock (e.g. inside _close_internal handling MSG_CHANNEL_CLOSE), so later SSHClient ops hang.

Override __del__ to try-acquire the channel lock: if the lock is already held, skip the lock-taking close (the holder typically sends EOF itself); if free, close normally. Explicit close() is unchanged.

Tests in tests/test_channelfile.py cover the deadlock case and free-lock GC shutdown. Changelog entry added.

The stdin file finalizer called shutdown_write(), which blocks on the
channel lock. GC can run that finalizer on the Transport thread while
it already holds the lock (for example inside _close_internal), hanging
later SSHClient operations. Skip the lock-taking close from __del__
when the lock is already held; explicit close() still sends EOF.

Co-authored-by: CenFangyu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] - exec_command can deadlock transport when its ChannelStdinFile is garbage collected at the wrong time

2 participants