fix: initialise StreamBase::lock_fd to -1 - #5035
Merged
connortechnology merged 1 commit intoAug 3, 2026
Merged
Conversation
lock_fd was initialised to 0, but every other use in the class treats a negative value as "no lock held": openComms() sets it to -1 when open() or flock() fails, and closeComms() guards on `lock_fd >= 0` before closing. The constructor's 0 passes that guard, so a StreamBase destructed without openComms() having succeeded calls close(0) and closes stdin. Reaching it only takes connkey > 0 plus a runStream() that returns before openComms(), and MonitorStream::runStream() has two such returns: the STREAM_SINGLE branch and the !monitor branch. mode=single URLs still carry a connkey, so both are reachable in normal operation. zms exits shortly afterwards and does little in between, so the practical impact today is small. It is still closing a descriptor the class does not own, and once fd 0 is free the next open() in the process silently lands on it. Add a regression test covering destruction with and without a connkey. It saves and restores fd 0 so a failure can't cascade into the rest of the suite. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Split out of the investigation in #5029. Independent of #5033 and #5034.
StreamBase::lock_fdwas initialised to0, but every other use in the class treats a negative value as "no lock held" —openComms()sets it to-1whenopen()orflock()fails, andcloseComms()guards onlock_fd >= 0before closing.The constructor's
0passes that guard, so aStreamBasedestructed withoutopenComms()having succeeded callsclose(0)and closes stdin.Reaching it takes
connkey > 0plus arunStream()that returns beforeopenComms(), andMonitorStream::runStream()has two such returns: theSTREAM_SINGLEbranch and the!monitorbranch.mode=singleURLs still carry a connkey, so both are reachable in normal operation.Severity, honestly: low today. zms exits shortly afterwards and does little in between. But it is closing a descriptor the class doesn't own, and once fd 0 is free the next
open()in the process silently lands on it — the kind of thing that turns into a confusing bug later rather than now.Adds a regression test with a minimal concrete
StreamBaseso the base constructor and destructor can run without a monitor, database, or shared memory. It saves and restores fd 0, so if it ever fails it reports one section rather than cascading into unrelated tests in the shared binary.Testing: verified the test fails with
lock_fd(0)restored and passes with the fix. Full suite: 117 test cases, 1788 assertions, all passing (Debug,BUILD_TEST_SUITE=ON). Notezm_fontneeds to run withtests/as cwd — it uses relativedata/fonts/paths — which is a pre-existing quirk, not related to this change.