Take the lock when closing a script log - #1284
Open
akirayamamoto wants to merge 1 commit into
Open
Conversation
Every write to the log holds this lock, and the read in GetOutput holds it too, but closing did not. Closing could therefore interleave with a write in progress, and closing does more than release the handle: it completes any JSON array left open. Racing that against a half written entry can leave malformed JSON on disk, which the reader then fails on.
akirayamamoto
marked this pull request as ready for review
August 13, 2026 06:15
| json.Close(); | ||
| writer.Dispose(); | ||
| writeStream.Dispose(); | ||
| lock (sync) |
Contributor
There was a problem hiding this comment.
It's interesting that this is the same lock object as the parent ScriptLog.
I don't know if disposing the json or writer objects cause changes to the underlying file. Maybe they do. If so, then yes, we would want disposing and reading (in ScriptLog) to wait for each other.
Author
There was a problem hiding this comment.
Yes, they do. I checked with a small repro: closing the writer with an array still open finishes it off on disk, because AutoCompleteOnClose defaults to true. Disposing the StreamWriter also flushes whatever is buffered.
sburmanoctopus
approved these changes
Aug 13, 2026
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.
Background
ScriptLogguards access to one log file with a single lock. Reads take it and writes take it, but closing does not. Closing is not inert either, since it completes any JSON array still open, so in principle a close could overlap a write that is partway through.I came across this while looking at a nightly that failed reading a corrupt script log, build 23575913. That has happened once and I could not tie it to this, so it is more where I came across the gap than a reason to merge.
Results
Closing now takes the same lock as writes, so a close and a write cannot overlap.
Nothing else changes. A corrupt log still fails the caller as it does today, and writes that arrive after the log has closed are untouched.
How to review this PR
Small enough to read in full.
Two things I would welcome a second opinion on. There is no
disposedflag, so a double dispose calls through twice, which looked harmless when I tried it. AndDisposenow waits on a write, though it runs on a background task rather than a request thread.