Fix SocketHandler firing writingTimeout instantly instead of waiting - #2071
Open
wakqasahmed wants to merge 2 commits into
Open
wakqasahmed wants to merge 2 commits into
wakqasahmed wants to merge 2 commits into
Conversation
lastWritingAt was left null when a fresh write began, so if the very first fwrite() made no progress, the stall check compared microtime() against (float) null (0.0) and always treated the timeout as already elapsed. Reset lastWritingAt alongside lastSentBytes at the top of writeToSocket() so the first stall check has a real baseline.
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.
SocketHandler::writeToSocket()resets$lastSentBytesto 0 at the top of every call, but never touches$lastWritingAt, which starts out asnull. If the very firstfwrite()in a call makes zero progress,writingIsTimedOut()sees$sent === $lastSentBytes(both 0) and takes the stalled branch, then comparesmicrotime(true)against(float) $this->lastWritingAt. Since that's still null, it casts to 0.0, so the elapsed-time check is always true on the very first stall — the socket gets closed and a timeout exception is thrown in a fraction of a millisecond, instead of after the configuredwritingTimeoutseconds.There's already a test (
testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds) that mocksfwriteto always return 0, but it only asserts that aRuntimeExceptionis eventually thrown, not when — so it's been passing the whole time despite the timeout never actually being honored.Fix is a one-line initialization: set
$lastWritingAt = microtime(true)alongside the existing$lastSentBytesreset, so the first stall check has a real baseline to measure from.Added a new test that mocks the same always-stalled
fwrite/streamGetMetadatascenario but asserts on timing: it sets a shortwritingTimeout, times how longwriteToSockettakes to throw, and checks that the elapsed time is actually close to the configured timeout rather than near-zero. I verified this test fails against the current code (throws in ~0.0007s against a 0.3s timeout) and passes with the fix applied.Ran the full
SocketHandlerTestsuite locally (PHP 8.3): 21 passed, 0 failed.