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

Add AllowCallWithBlock option to ThreadSafety/DirChdir cop #73

Conversation

viralpraxis
Copy link
Collaborator

Using Dir.chdir with provided block is much more safer than block-less call since it restores PWD to initial state . I think it makes sense to add an option to allow these safe calls.

@viralpraxis viralpraxis self-assigned this Jan 10, 2025
Using `Dir.chdir` with provided block is much more safer
than block-less call since it restores `PWD` to initial state . I think it makes sense to add an option
to allow these safe calls.
@viralpraxis viralpraxis force-pushed the add-allow-call-with-block-option-to-dir-chdir-cop branch from 68ccb1f to a2184ae Compare January 10, 2025 20:14
@viralpraxis
Copy link
Collaborator Author

@mikegee could you please take a look? 🙂

@mikegee
Copy link
Collaborator

mikegee commented Jan 11, 2025

I definitely support using a block to restore PWD. That's awesome. 👍

I don't understand why we would consider it thread-safe. Am I missing something?

@viralpraxis
Copy link
Collaborator Author

viralpraxis commented Jan 12, 2025

I have a gem I'm currently developing; I'm confident that block-ful chdir is safe in my case so I don't wont it to be marked as on offense, but I still want block-less calls to be detected since it is permanent global state modification leading to bugs, e.g. if I forget to restore PWD. So I think there should be an option between disabling this cop and leaving it's configuration as-is. Flag AllowCallWithBlock solves this problem. So, there will be 3 configuration variants:

  1. Gem is disabled, nothing is detected
  2. AllowCallWithBlock is enabled, only block-ful calls are allowed
  3. Default configuration, no chdir is allowed

@mikegee
Copy link
Collaborator

mikegee commented Jan 12, 2025

Ok, I can support the option. As long as it is not the default configuration and there is sufficient warning that using a block does not make the code thread-safe.

I also wonder if it is possible to write app code in/around that block to guarantee thread-safety. Maybe with a global mutex? Then the cop could detect/suggest that pattern. That would be extra awesome.

@viralpraxis
Copy link
Collaborator Author

viralpraxis commented Jan 12, 2025

Ok, I can support the option. As long as it is not the default configuration and there is sufficient warning that using a block does not make the code thread-safe.

Great. I seem some room for cop documentation improvements, I'll address it in another PR.

I also wonder if it is possible to write app code in/around that block to guarantee thread-safety. Maybe with a global mutex? Then the cop could detect/suggest that pattern. That would be extra awesome.

That's a good point! I'll into that.

After merging this we'll be able to ship a new version. This is important since I want to finish and merge #70 before releasing 0.7. That documentation files must be accessible with a git tag so it will be picked by docs.rubocop.org'.

@viralpraxis viralpraxis merged commit 4176ff7 into rubocop:master Jan 12, 2025
22 checks passed
@viralpraxis viralpraxis deleted the add-allow-call-with-block-option-to-dir-chdir-cop branch January 12, 2025 17:30
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.

2 participants