Repository navigation
Conversation
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]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| if not lock.acquire(blocking=False): | ||
| return |
There was a problem hiding this comment.
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 👍 / 👎.
Fixes paramiko#2685.
Problem
ChannelStdinFileinheritedBufferedFile.__del__, which callsclose()→shutdown_write()→ blockingChannel.lock.acquire(). Garbage collection can run that finalizer on the Transport thread while it already holds the same lock (for example insideChannel._close_internalhandlingMSG_CHANNEL_CLOSE). The Transport thread then self-deadlocks and laterSSHClientoperations hang until timeout.Change
Override
ChannelStdinFile.__del__so it try-acquires the channel lock:_close_internal, which sends EOF itself.Explicit
close()is unchanged and still callsshutdown_write().This is narrower than switching
Channel.lockto anRLock. A reentrant lock would let the finalizer call_send_user_messagewhile 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
main(AssertionError: ChannelStdinFile finalizer deadlockedafter 2s).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.