Open
Conversation
Merged
There was a problem hiding this comment.
Pull request overview
Applies the configured PHP stream timeout to the libssh2 session during SSH channel cleanup so fclose()/shutdown won’t hang indefinitely when libssh2_channel_free() blocks waiting for SSH_MSG_CHANNEL_CLOSE.
Changes:
- Set
libssh2_session_set_timeout()to the channel stream’s timeout immediately beforelibssh2_channel_free(), and clear it after (guarded byPHP_SSH2_SESSION_TIMEOUT). - Add a PHPT regression test that ensures
ssh2_exec()channel close returns within a bounded time when a stream timeout is set.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
ssh2_fopen_wrappers.c |
Applies the stream timeout around the channel close/free path to prevent indefinite blocking. |
tests/ssh2_close_timeout.phpt |
Adds coverage to verify fclose() respects the configured stream timeout for long-running remote commands. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Apply session timeout during channel close
Problem
When a PHP script opens an SSH channel via
ssh2_exec()(or similar), thechannel close path can block indefinitely.
libssh2_channel_free()callslibssh2_channel_close()internally, which waits for the remote end to sendSSH_MSG_CHANNEL_CLOSE. If the remote command hasn't finished or theconnection has been lost, this blocks forever.
The session timeout set via
stream_set_timeout()is only applied aroundexplicit stream reads and writes but not during the close path. So even with a
timeout configured, channel cleanup during script shutdown hangs.
This also means
max_execution_time/set_time_limit()won't help sincethe process is blocked in a socket
recv(), not consuming CPU time.Fix
Apply the channel's timeout to the libssh2 session before calling
libssh2_channel_free(), and clear it after. This matches the existingpattern used in the read and write stream ops. If the close blocks longer
than the configured timeout, libssh2 returns
LIBSSH2_ERROR_TIMEOUTinstead of waiting forever.
Guarded by
#ifdef PHP_SSH2_SESSION_TIMEOUTfor compatibility with olderlibssh2 versions that lack
libssh2_session_set_timeout().Reproducer