Skip to content

test(kernel): stop leaked drain-test goroutines writing into TestLogSinkForwardMapsLevels' log buffer - #559

Merged
vuanhphung merged 2 commits into
mainfrom
fix/flaky-logsink-test
Nov 13, 2026
Merged

vuanhphung merged 2 commits into
mainfrom
fix/flaky-logsink-test

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

TestLogSinkForwardMapsLevels fails intermittently in CI with invalid character '{' after top-level value (seen in the kernel-backend lane on #557). The test points the shared driver logger at a buffer and expects exactly one JSON record. A second record lands in that buffer: {"level":"info","target":"databricks::sql::kernel","message":"after"}. The previous test, TestDrainContainsPanicFromForwardAndDropWarning, enqueues that record.

The two drain tests started drainKernelLogs with go ...; defer close(ch) and never waited for the goroutine to exit. Their observe hook signals the test before forward writes the record. The drain's last write can therefore happen after the test returns, by which time the next test has retargeted the shared output to its own buffer. The race detector reports the same thing as a data race on that bytes.Buffer, with the writer created at logforward_async_test.go:127 (and :78 for TestDrainRecoversFromWriterPanic). This is test isolation, not a production leak: production starts the drain once per process by design, because the kernel's tracing subscriber is process-wide and first-call-wins.

The fix is a startDrain test helper. At cleanup it closes the channel and waits (bounded at 5s) for the drain to return. That cleanup runs before the earlier-registered cleanup restores the shared output, and in the queue test the queue is unpublished before the channel is closed. No production code and no assertions change. TestLogSinkFollowsLocalFileRetarget and TestKernelLogCtxEmitsCorrelation also swap the logger, but neither starts a goroutine. They were only victims of the leaked drains, so they're unchanged.

Verification. Prebuilt test binaries, 32 instances pinned to 2 CPUs to mimic a contended runner, -run 'TestDrain|TestLogSink', main and branch alternated:

Build main failures branch failures
tagged databricks_kernel (CI lane config) 9 / 128,000 0 / 128,000
tagged + -race 163 / 96,000 0 / 96,000
untagged + -race 175 / 96,000 0 / 96,000

On an idle machine main passes 0/500, which is why this only shows up in CI. A scratch test that widens the observe→write window deterministically fails with the old pattern and passes with startDrain. On the branch with -race, these are clean: -count=500 of TestLogSink and of TestDrain|TestLogSink (tagged and untagged), and -count=50 of the whole package (tagged and untagged). make test and make lint pass. The change is test-only, so there's no performance or e2e effect.

Cross-driver state: Not applicable (Go-internal test hygiene).

Adversarial review: converged in round 1 with no findings.

No issue filed; seen on #557's CI.

This pull request and its description were written by Isaac.

TestDrainRecoversFromWriterPanic and TestDrainContainsPanicFromForwardAndDropWarning
started drainKernelLogs without waiting for it to exit. observe signals the test
before forward writes the record, so the drain's final write could land after the
test returned, in the buffer TestLogSinkForwardMapsLevels had just installed
(CI: "invalid character '{' after top-level value"; the stray record was the
drain test's {"message":"after"}).

A startDrain helper now closes the channel and waits for the drain at cleanup,
before the earlier-registered cleanup restores the shared output.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
@vuanhphung
vuanhphung marked this pull request as ready for review November 13, 2026 18:53

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

@vuanhphung
vuanhphung enabled auto-merge November 13, 2026 23:23
@vuanhphung
vuanhphung added this pull request to the merge queue Nov 13, 2026
Merged via the queue into main with commit 1c37623 Nov 13, 2026
14 checks passed
@vuanhphung
vuanhphung deleted the fix/flaky-logsink-test branch November 13, 2026 23:34
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.

1 participant