Skip to content

Fix failed and stalled reads on secure connections - #39

Merged
GrahamCampbell merged 2 commits into
1.9from
fix-secure-reads
Nov 25, 2026
Merged

GrahamCampbell merged 2 commits into
1.9from
fix-secure-reads

Conversation

@GrahamCampbell

@GrahamCampbell GrahamCampbell commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Since 1.8.1 the client has failed to connect or stalled for the socket timeout on wss:// connections, as reported in #36. There are two causes. A readable socket does not always hold application data on an encrypted stream, because TLS 1.3 session tickets and partly received records make it readable too, so waiting once and then reading once could return nothing and fail the handshake. And polling with stream_select() before a blocking fread() lets PHP move decrypted bytes into its own buffer, after which PHP before 8.4.24 and 8.5.9 blocks for the whole socket timeout while topping that buffer up. On PHP before 8.2.19 and 8.3.7 the feof() call can also wait indefinitely on an incomplete record.

receive() now reads in non-blocking mode and restores blocking mode afterwards, so it cannot wait on any PHP version, and when given a wait time it keeps waiting until application data arrives or the time runs out. As before, it reads once from the socket and carries on only while PHP still holds buffered bytes, so a read from an unbuffered stream, which is what the server accepts, stays bounded by the length asked for, and a single call is now also capped at 64 KiB. connect() reads until the end of the response headers instead of reading once, which also fixes handshakes whose response arrives in more than one read, and it passes anything that follows the headers to the payload handler so that a frame sent together with the response is no longer lost. Payloads still queued from an earlier connection are discarded when connecting again. On the server, an empty read from a socket that is still connected is no longer treated as the client closing the connection.

The new tests use a TLS connection set up inside the test process, whose encrypted bytes reach the client only when the test delivers them, so an incomplete record can be reproduced without a second process.

@GrahamCampbell
GrahamCampbell marked this pull request as ready for review November 25, 2026 23:34
@GrahamCampbell
GrahamCampbell merged commit 131a748 into 1.9 Nov 25, 2026
20 checks passed
@GrahamCampbell
GrahamCampbell deleted the fix-secure-reads branch November 25, 2026 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant