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

Rspec spec does not terminate #164

Closed
klobuczek opened this issue Jun 8, 2022 · 2 comments
Closed

Rspec spec does not terminate #164

klobuczek opened this issue Jun 8, 2022 · 2 comments

Comments

@klobuczek
Copy link

I cannot figure out why the spec in the 2nd context does not terminate:

RSpec.describe Spec do
  context 'terminates' do
    let(:s) { Sync { 1 } }

    it 'eq' do
      expect(s).to eq 1
    end
  end

  context 'does not terminate' do
    let(:s) { Sync { param } }
    let(:param) { 1 }

    it 'eq' do
      expect(s).to eq 1
    end
  end
end

Using ruby 3.1.2 and async 2.0.2.

@ioquatix
Copy link
Member

ioquatix commented Jun 9, 2022

IIRC, this is because let blocks are "thread safe" and have an implicit shared mutex around every request. It's supposed to be reentrant but because Sync { param } happens on a different fiber, it's effectively a deadlock, i.e. it's not that different from:

m = Thread::Monitor.new # recursive mutex

param = lambda{m.synchronize{1}}
s = lambda{m.synchronise{Thread.new{param.call}.value}}

s.call

To resolve s it locks the mutex for the current (main) thread. Then it creates a new thread (Sync block creates a new fiber) and then tries to lock the mutex again and this deadlocks.

The real issue here is whether RSpec should be "thread safe" in this way. I personally think it's wrong.

@ioquatix
Copy link
Member

I got pretty frustrated with RSpec for this reason as well as others, and implemented https://github.com/ioquatix/sus which explicitly avoids this problem. I don't have a good answer and can't fix RSpec, maybe they will avoid having a mutex around their let variables.

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

No branches or pull requests

2 participants