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

Make global stubs thread-safe #908

Merged
merged 1 commit into from Oct 12, 2020
Merged

Conversation

adam-harwood
Copy link
Contributor

@adam-harwood adam-harwood commented Oct 12, 2020

This fixes an issue where testing multi-threaded code could sometimes result in incorrect responses being returned. Specifically in the case of VCR, since it seems to mock a nil response first before stubbing the real value, a nil response could sometimes be returned. This was because the hash being used by global stubs is not thread-safe.

I tried to write a test for this, but given the multi-threaded nature, and the fact it doesn't happen on MRI, it was difficult to find anything that failed. The existing tests do at least regression test it though. And I've tested it extensively on the repo where I originally found the issue and it works well.

closes #907

This fixes an issue where testing multi-threaded code could sometimes result in incorrect responses being returned. Specifically in the case of VCR, since it seems to mock a nil response first before stubbing the real value, a nil response could sometimes be returned. This was because the hash being used by global stubs is not thread-safe.

I tried to write a test for this, but given the multi-threaded nature, and the fact it doesn't happen on MRI, it was difficult to find anything that failed. The existing tests do at least regression test it though.

closes bblimke#907
@bblimke
Copy link
Owner

bblimke commented Oct 12, 2020

Thank you @adam-harwood 👍

@bblimke bblimke merged commit d03e76a into bblimke:master Oct 12, 2020
@bblimke
Copy link
Owner

bblimke commented Oct 12, 2020

It's released as 3.9.2

@adam-harwood
Copy link
Contributor Author

It's released as 3.9.2

Thank you for the quick release!

adam-harwood pushed a commit to adam-harwood/webmock that referenced this pull request Oct 16, 2020
httpclient_adapter uses some standard Ruby hashes, which produce unexpected results when working in a true multi-threaded (e.g. JRuby) environment. Multiple threads doing puts and deletes on these hashes result in nils sometimes being returned for a properly stubbed request. These errors would manifest as:
webmock-3.9.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:113:in `build_httpclient_response': undefined method `body' for nil:NilClass (NoMethodError)

Unfortunately these errors are extremly difficult to replicate.

This PR is a follow-on from bblimke#908, and represents the last thread-safety issue we've been able to find.
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.

Global stubs are not thread-safe
2 participants