Repository navigation
Conversation
append_to_jsonl opened the file in append mode and wrote each record without any locking. This means that threads logging to the same jsonl file (e.g. trainer.diagnose with several workers) could interleave and leave a truncated line. Add a regression test that appends from 8 threads and checks every line parses. Protect the write with a module-level lock.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
objective of this PR?
append_to_jsonljust callsjsonlines.open(f, mode="a")and writes one record per call. If multiple threads are writing to the same file (liketrainer.diagnoserun with multiple workers), their writes can interleave and a line gets chopped up by the next record. That is the corrupt line reported in #232.This puts a module-level lock around the append so that threads in one process write one record at a time. The record is serialised (
to_dict) outside the lock so the critical section is just the file write. Separate processes writing to the same file are still not coordinated; I noted that in the docstring.Closes #232
How did I verify it?
Reproduction: 400 records with 20k char payload each, 8 threads appending.
Before the change, 253 of 400 lines were not valid JSON. After it, all 400 are.
new test
adalflow/tests/test_file_io.py::test_append_to_jsonl_is_thread_safe(200 records, 8 threads). Without the change it fails (JSONDecodeError: Extra data) and with it it passes.tests/test_file_io.pyandtests/test_tracing.py: 34 passedblack --line-length=88andruff(versions pinned in.pre-commit-config.yaml) succeed on the changed files.Things to know
I couldn't tell if this was the exact cause of the initial report; the thread above cites a bad file from a previous run as another possibility. This fixes the thread race I was able to reproduce.
Before Submitting