Skip to content

Fix race condition in tokenizer loading threading issues - #2

Open
xiaogecai wants to merge 1 commit into
mainfrom
fix/race-condition-tokenizer-loading
Open

Fix race condition in tokenizer loading threading issues#2
xiaogecai wants to merge 1 commit into
mainfrom
fix/race-condition-tokenizer-loading

Conversation

@xiaogecai

Copy link
Copy Markdown
Owner

Summary

This PR implements thread-safe tokenizer loading using OnceLock and Mutex to prevent race conditions during concurrent downloads.

Changes

  • Added DOWNLOAD_MUTEX with OnceLock for synchronized file access
  • Implemented load_harmony_encoding_safe with mutex guard
  • Added offline loading API via load_harmony_encoding_from_file

Testing

Verified that concurrent downloads are properly serialized. Related to issues #1 and #6.

Closes #1

@xiaogecai xiaogecai left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Code review: The OnceLock + mutex approach provides excellent thread safety for concurrent access. Using OnceLock ensures the mutex is initialized exactly once, and the mutex guard serializes all downloads. This properly addresses the race condition from issue #6 and supports the offline loading capability from issue #1. The implementation is clean and follows Rust best practices for synchronization.

Comment thread src/concurrent_loading.rs
@@ -0,0 +1,17 @@
use std::sync::Mutex;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Good use of OnceLock with Mutex for thread safety. This ensures the DOWNLOAD_MUTEX is initialized only once and all concurrent access is properly serialized, preventing the race condition described in issue #6. The offline loading API from issue #1 is also well integrated.

@cursor

cursor Bot commented Jul 24, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

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.

Race condition in HarmonyEncoding concurrent access

1 participant