Skip to content

Take the lock when closing a script log - #1284

Open
akirayamamoto wants to merge 1 commit into
OctopusDeploy:mainfrom
akirayamamoto:akirayamamoto/close-script-log-under-lock
Open

Take the lock when closing a script log#1284
akirayamamoto wants to merge 1 commit into
OctopusDeploy:mainfrom
akirayamamoto:akirayamamoto/close-script-log-under-lock

Conversation

@akirayamamoto

Copy link
Copy Markdown

Background

ScriptLog guards 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 disposed flag, so a double dispose calls through twice, which looked harmless when I tried it. And Dispose now waits on a write, though it runs on a background task rather than a request thread.

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
akirayamamoto marked this pull request as ready for review August 13, 2026 06:15
@akirayamamoto
akirayamamoto requested a review from a team as a code owner August 13, 2026 06:15
json.Close();
writer.Dispose();
writeStream.Dispose();
lock (sync)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants