Skip to content

fix: race condition when another process access simo file - #4

Merged
bruhtus merged 3 commits into
masterfrom
fix/race-condition-when-another-process-access-simo-file
Aug 31, 2026
Merged

bruhtus merged 3 commits into
masterfrom
fix/race-condition-when-another-process-access-simo-file

Conversation

@bruhtus

@bruhtus bruhtus commented Aug 31, 2026

Copy link
Copy Markdown
Owner

With the wrong timing, when another process try to access simo file at
the same time when simo updating the file content (i.e pause/resume,
change to focus/break session), those another process might read
incomplete json file when trying to unmarshal the content.

So write the new info in temporary file first, and then replace the old
file with these temporary file to prevent another process from reading
incomplete json file.

With the wrong timing, when another process try to access simo file at
the same time when `simo` updating the file content (i.e pause/resume,
change to focus/break session), those another process might read
incomplete json file when trying to unmarshal the content.

So write the new info in temporary file first, and then replace the old
file with these temporary file to prevent another process from reading
incomplete json file.
The problem is that using os.File Write() and Truncate() might not
replace the current data with the new data, which means the data in simo
file for test will be stale and that causing some of the test cases to
fail when we run multiple test cases.

Using os.WriteFile() solve that by using OpenFile() with O_TRUNC option
underneath. Still not sure what is the difference here, but this is
something to keep in mind.
Rather than hard coded the expected output, check the actual isNotify
value is the same as the input value or not. This reduces the overhead
of maintaining the expected output, and prevent print out misinformation
when the actual isNotify value is not the same as input file. For
example, "Got false, want false" if the isNotifyInput and isNotifyOutput
is false but the actual isNotify value is true.
@bruhtus
bruhtus merged commit 61f7573 into master Aug 31, 2026
1 check passed
@bruhtus
bruhtus deleted the fix/race-condition-when-another-process-access-simo-file branch August 31, 2026 05:17
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