Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:43:41+00:00 2026-06-07T11:43:41+00:00

My API allows users to buy certain unique items, where each item can only

  • 0

My API allows users to buy certain unique items, where each item can only be sold to one user. So when multiple users try to buy the same item, one user should get the response: ok and the other user should get the response too_late.

Now, there seems to be bug in my code. A race condition. If two users try to buy the same item at the same time, they both get the answer ok. The issue is clearly reproducable in production. Now I have written a simple test that tries to reproduce it via rspec:

context "when I try to provoke a race condition" do
  # ...

  before do
    @concurrent_requests = 2.times.map do
      Thread.new do
        Thread.current[:answer] =  post "/api/v1/item/buy.json", :id => item.id
      end
    end

    @answers = @concurrent_requests.map do |th|
      th.join
      th[:answer].body
    end
  end

  it "should only sell the item to one user" do
    @answers.sort.should == ["ok", "too_late"].sort
  end
end

It seems like does not execute the queries at the same time. To test this, I put the following code into my controller action:

puts "Is it concurrent?"
sleep 0.2
puts "Oh Noez."

Expected output would be, if the requests are concurrent:

Is it concurrent?
Is it concurrent?
Oh Noez.
Oh Noez.

However, I get the following output:

Is it concurrent?
Oh Noez.
Is it concurrent?
Oh Noez.

Which tells me, that capybara requests are not run at the same time, but one at a time. How do I make my capabara requests concurrent?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:43:42+00:00Added an answer on June 7, 2026 at 11:43 am

    Multithreading and capybara does not work, because Capabara uses a seperate server thread which handles connection sequentially. But if you fork, it works.

    I am using exit codes as an inter-process communication mechanism. If you do more complex stuff, you may want to use sockets.

    This is my quick and dirty hack:

    before do
      @concurrent_requests = 2.times.map do
        fork do
          # ActiveRecord explodes when you do not re-establish the sockets
          ActiveRecord::Base.connection.reconnect!
    
          answer = post "/api/v1/item/buy.json", :id => item.id
    
          # Calling exit! instead of exit so we do not invoke any rspec's `at_exit`
          # handlers, which cleans up, measures code coverage and make things explode.
          case JSON.parse(answer.body)["status"]
            when "accepted"
              exit! 128
            when "too_late"
              exit! 129
          end
        end
      end
    
      # Wait for the two requests to finish and get the exit codes.
      @exitcodes = @concurrent_requests.map do |pid|
        Process.waitpid(pid)
        $?.exitstatus
      end
    
      # Also reconnect in the main process, just in case things go wrong...
      ActiveRecord::Base.connection.reconnect!
    
      # And reload the item that has been modified by the seperate processs,
      # for use in later `it` blocks.
      item.reload
    end
    
    it "should only accept one of two concurrent requests" do
      @exitcodes.sort.should == [128, 129]
    end
    

    I use rather exotic exit codes like 128 and 129, because processes exit with code 0 if the case block is not reached and 1 if an exception occurs. Both should not happen. So by using higher codes, I notice when things go wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to offer a web API service that allows users to download
We have a CMS that supports multiple sites, one of our features allows our
I'm using Users Points Voting API module to combine User Points module and Fivestar
We're writing an API that allows users to sign up for a Joomla account
I'm building a site that allows users to create a profile. Each profile page
I am writing an internal API for my company which allows users to pass
The recaptcha API allows pure JavaScript captcha creating and verifying . But, how can
I have a TreeView that allows users to select certain elements of hierarchical data
I'm using the P4PHP API to build a GUI that allows users to mass
I have a simple page which allows users to add language ids to items.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.