Skip to content

Fix ChannelStdinFile GC deadlock when channel lock is held (#2685) - #1

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

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

Conversation

@Dmao233

@Dmao233 Dmao233 commented Sep 7, 2026 •

Copy link
Copy Markdown
Owner

Fixes paramiko#2685.

Problem

ChannelStdinFile inherited BufferedFile.__del__, which calls close() → shutdown_write() → blocking Channel.lock.acquire(). Garbage collection can run that finalizer on the Transport thread while it already holds the same lock (for example inside Channel._close_internal handling MSG_CHANNEL_CLOSE). The Transport thread then self-deadlocks and later SSHClient operations hang until timeout.

Change

Override ChannelStdinFile.__del__ so it try-acquires the channel lock:

  • If the lock is already held, skip the lock-taking close. The holder is typically _close_internal, which sends EOF itself.
  • If the lock is free, close normally so GC still shuts down the write side.

Explicit close() is unchanged and still calls shutdown_write().

This is narrower than switching Channel.lock to an RLock. A reentrant lock would let the finalizer call _send_user_message while the outer holder still owns the lock, which this codebase avoids because it can deadlock during re-keying.

Tests

  • test_gc_does_not_deadlock_while_channel_lock_is_held — reference cycle + gc.collect() while the lock is held, then _close_internal. Failed by timeout before the fix; passes after.
  • test_gc_still_shuts_down_write_when_lock_is_free — GC still sends write-side EOF when the lock is available.

No live sshd required.

Validation

  • Regression failed on current main (AssertionError: ChannelStdinFile finalizer deadlocked after 2s).
  • After the fix: pytest tests/test_channelfile.py tests/test_transport.py — 108 passed, 2 skipped.
  • pytest tests/test_file.py tests/test_client.py tests/test_buffered_pipe.py — 51 passed, 3 skipped.
  • flake8 paramiko/channel.py tests/test_channelfile.py — clean.

Notes

This PR is on the Dmao233 fork. An upstream PR against Paramiko/paramiko will be opened separately.

Open in Web Open in Cursor 

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]>
@Dmao233
Dmao233 marked this pull request as ready for review September 7, 2026 01:35
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-07T01:38:23.532523Z d7e884b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d7e884b191

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread paramiko/channel.py
Comment on lines +1401 to +1402
if not lock.acquire(blocking=False):
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve finalization when another thread owns the lock

acquire(blocking=False) returns false whether this finalizer's thread owns the lock or a different thread merely holds it temporarily. Since Channel.lock also protects ordinary operations such as sends and channel handlers, cyclic GC on one thread can encounter routine contention from another and return here permanently; the one-shot finalizer then neither flushes buffered stdin nor sends EOF, potentially leaving the remote command waiting indefinitely. The skip should be limited to the self-deadlock/closed-channel case rather than treating all lock contention as completed cleanup.

Useful? React with 👍 / 👎.

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