Fix corrupted JSONL metrics file due to concurrent writes#19011
Conversation
Summary of ChangesHello @talorabr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical data integrity issue where concurrent writes to JSONL metrics files could lead to corruption. By introducing a read-write lock, the system now safely handles parallel write operations, ensuring that all exported metrics data remains valid and uncorrupted, which is crucial for reliable performance monitoring. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a potential race condition that could lead to corrupted JSONL metrics files from concurrent writes. The use of a lock to serialize file access within FileRequestMetricsExporter.write_record is the appropriate solution. The implementation is clean and effectively resolves the issue. My review includes a suggestion to simplify the locking mechanism by using a standard asyncio.Lock instead of an RWLock, as only exclusive write access is currently required.
|
|
||
| from sglang.srt.managers.io_struct import EmbeddingReqInput, GenerateReqInput | ||
| from sglang.srt.server_args import ServerArgs | ||
| from sglang.srt.utils.aio_rwlock import RWLock |
There was a problem hiding this comment.
@talorabr Can we replace with a simpler asyncio.Lock as suggested
|
|
||
| # File handler state management | ||
| self._current_file_handler = None | ||
| self._current_file_lock = RWLock() |
There was a problem hiding this comment.
Using a standard asyncio.Lock is simpler here since there are no concurrent read operations. While RWLock is not incorrect, asyncio.Lock is more idiomatic for purely exclusive access. If you anticipate adding concurrent read-only operations in the future, keeping RWLock is reasonable.
| self._current_file_lock = RWLock() | |
| self._current_file_lock = asyncio.Lock() |
|
|
||
| # Ensure correct file handler is open for current hour | ||
| self._ensure_file_handler(hour_suffix) | ||
| async with self._current_file_lock.writer_lock: |
|
Do we have example of json file before/after this PR as comparison |
sglang-request-metrics-20260222_04.after.log In the |
Motivation
When using sglang server with
--export-metrics-to-file --export-metrics-to-file-dir /tmp/sglang-metrics, concurrent writes can corrupt the jsonl file of the metrics.Example of corrupted jsonl file with 32 concurrent requests:
Write lock mechanism will prevent this from happening.
Modifications
Added
RWLockfor theFileRequestMetricsExporter, which is acquired when we open the metrics file in_write_record.Accuracy Tests
Not expected to impact model output/accuracy.
Benchmarking and Profiling
Not expected to impact latency/throughput.
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci