-
Notifications
You must be signed in to change notification settings - Fork 14
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
Mocks panic when accessed from multiple threads #35
Comments
Hm, this is kind of tricky.
But looking at your example, doing a What do you think? |
One issue I can think of is that a global lock can cause a deadlock when calling different methods. Something like that: faux::when!(foo.baz).then(|()| { a.lock(); a.unlock(); });
fn thread1() {
a.lock();
foo.bar();
a.unlock();
}
fn thread2() {
foo.baz():
} First thread1 locks a, then thread2 calls baz and waits, then thread1 calls bar and we deadlock. One way to alleviate that could be to wrap each mocked method in an Arc<Mutex<_>> so we can clone the method out of MockStore and release its lock immediately. That protects us from most surprises. |
I don't understand how an For what it's worth, |
From what I understand, the MockStore lock is taken for the entire duration of a call, so it synchronizes access to all mock's methods, causing an unexpected dealock described above. The idea behind storing an Arc<Mutex<_>> is to grab the MockStore mutex only to clone the Arc out, then hold the method's mutex during the call. It doesn't eliminate deadlocks entirely, but it no longer locks the entire object, so playing around with sleeping/locking inside a mock method is less likely to cause unexpected deadlocks. |
Ohh I see, that's an interesting thought. That gets a little tricky as it needs to be wrapped in a And of course I welcome PRs if you beat me to the fix since I have been very focused on the new api changes lately, and I still have work to do there. |
This makes mocks no longer panic, while making sure that calling individual mocked methods does not lock the entire mock. For convenience, the mutex is moved inside MockStore. Fixes nrxus#35. Signed-off-by: Igor Kotrasinski <[email protected]>
I think #36 is a good start, though I think I completely ignored the mock consuming bit. |
On 0.0.8, this minimal example panics:
try_lock()
in morphed.rs seems to be the cause.The text was updated successfully, but these errors were encountered: