Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(init): Correctly lock run directories #209

Merged
merged 15 commits into from
Apr 6, 2020
Merged

fix(init): Correctly lock run directories #209

merged 15 commits into from
Apr 6, 2020

Conversation

Swatinem
Copy link
Member

@Swatinem Swatinem commented Mar 31, 2020

This allows multi-process usage, and also fixes file deletion on windows.

fixes #220
fixes #223

@Swatinem Swatinem requested a review from jan-auer March 31, 2020 15:03
`sentry_shutdown` now cleans up after itself, and the code is covered
by integration tests anyway.
@Swatinem Swatinem marked this pull request as ready for review March 31, 2020 15:20
Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

Disclaimer: Only looked at the UNIX implementation so far. I think this implementation is inherently racy and can only be solved by locking first. I'm not sure if you're flocking the file, you even need to check the PID?

For reference, here's an implementation in Python based on the C version:
https://github.com/benediktschmitt/py-filelock/blob/b30bdc4fb998f5f4350257235eb51147f9e81862/filelock.py#L377

And the corresponding stack overflow question:
https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition

@jan-auer jan-auer changed the title fix: Correctly lock run directories fix(init): Correctly lock run directories Apr 1, 2020
@cammm
Copy link
Contributor

cammm commented Apr 1, 2020

Same with windows, you don't need to write the PID, just make a call to the _locking function

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/locking?view=vs-2019

The example Jan linked is great and has a windows version too

https://github.com/benediktschmitt/py-filelock/blob/b30bdc4fb998f5f4350257235eb51147f9e81862/filelock.py#L338

Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

Preliminary review of the file locking logic. Thanks, this is looking very promising. I might have found a few issues still, see comments attached.

{
lock->is_locked = false;

int fd = open(lock->path->path, O_RDWR | O_CREAT | O_TRUNC,
Copy link
Member

Choose a reason for hiding this comment

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

You might want to pass O_EXCL here. Since unlock deletes the file first, this should be safe. Also, since you're not writing to the file, would it be sufficient to open with O_RDONLY?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think its both simpler and easier to understand without O_EXCL. The problem is with leftover files that were not correctly cleaned up. Like in the case of a crash.

Copy link
Member

@jan-auer jan-auer Apr 6, 2020

Choose a reason for hiding this comment

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

Good point, in case of a crash, we might consider to actually clean those up, wdyt?

Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

Phew, what a trip 😆 Looks good now, pretty excited to get this merged!

@Swatinem Swatinem merged commit 6a6e42f into master Apr 6, 2020
@Swatinem Swatinem deleted the fix/file-locks branch April 6, 2020 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants