Skip to content

Fix ThreadSanitizer data races between bundle processing and async background writer closing in FileBasedSink and WriteFiles - #39458

Open
stankiewicz wants to merge 1 commit into
apache:masterfrom
stankiewicz:tsan_filebasedsink
Open

Fix ThreadSanitizer data races between bundle processing and async background writer closing in FileBasedSink and WriteFiles#39458
stankiewicz wants to merge 1 commit into
apache:masterfrom
stankiewicz:tsan_filebasedsink

Conversation

@stankiewicz

@stankiewicz stankiewicz commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes ThreadSanitizer data races during asynchronous file closing in WriteFiles / FileBasedSink and an OS file-descriptor race in TFRecordSchemaTransformProviderTest.

Root Cause

  • Async Writer Close (FileBasedSink / WriteFiles): FileBasedSink.Writer instances are written to on the bundle processing thread and closed asynchronously via MoreFutures.runAsync. Lacking a release-acquire memory barrier between threads, TSAN reported cross-thread data races on:
    1. FileBasedSink.Writer.close()V (FileBasedSink.java:1066 reading channel.isOpen() vs. Writer.open() at FileBasedSink.java:993 writing channel = factory.create(...)).
    2. CRC32.getValue()J (reading GZIP checksums during close() vs. crc.update(...) during write(value)).
  • Test Resource Leak (TFRecordSchemaTransformProviderTest): An unclosed FileInputStream caused a syscall race when the JDK background Cleaner thread closed the unreferenced file descriptor during test execution.

Solution

  • Release-Acquire Handoff (readyToClose): Added an AtomicBoolean readyToClose handoff to FileBasedSink.Writer. Calling releaseForBackgroundClose() (readyToClose.set(true)) before spawning the async task and calling readyToClose.get() at the start of close()/cleanup() ensures that open(), write(), and all stream mutations happen-before background closing.
  • Try-With-Resources: Wrapped FileInputStream in TFRecordSchemaTransformProviderTest.runTestWrite in a try-with-resources block for deterministic closure.

Testing

  • All TSAN runs of TFRecordSchemaTransformProviderTest (--config=tsan-chlor) pass cleanly with zero data race reports.

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@github-actions github-actions Bot added the java label Jul 23, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @chamikaramj for label java.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@stankiewicz

Copy link
Copy Markdown
Contributor Author

R: @sjvanrossum

@github-actions

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@sjvanrossum

Copy link
Copy Markdown
Contributor

If I understand correctly, open(String) initializes all relevant fields which are thereafter never written to again, correct?
Calling open(String) concurrently looks unsafe, so ideally it should be marked as synchronized along with close() to ensure mutually exclusive initialization after construction and to ensure visibility of those writes.

Alternatively, attempt a CAS on id before writing any of the other fields for exclusive initialization after construction (note that visibility of the other fields is not yet guaranteed) followed by CAS on either resourceId or channel to establish a release barrier. That same field must be acquired (e.g., volatile get or getAcquire) before reading any other field in close().

…ckground writer closing in FileBasedSink and WriteFiles
@stankiewicz

Copy link
Copy Markdown
Contributor Author

@sjvanrossum It's not only open() and async close(), it is also racing between write() and async close. take a look at this approach.

@stankiewicz

Copy link
Copy Markdown
Contributor Author

R: @sjvanrossum

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants