rails test:system race condition(?) with webpacker Ruby

Steps to reproduce

1) Use webpacker 2) Have 4+ system tests 3) Run them in parallel after changing javascript files so webpacker needs to compile (parallelize(workers: :number_of_processors))

Expected behavior

It works

Actual behavior

Occasionally will get a JSON Parse error from trying to load the manifest resulting in the system tests failing.

Screen Shot 2019-10-15 at 2 47 17 PM

System configuration

Rails 6.0.0

Workaround / Fix

Not sure if this is a webpacker bug or a rails system tests bug.

I believe the following monkeypatch proves that it is a race condition as it fixes the issue.

ruby class Webpacker::Manifest def load if config.public_manifest_path.exist? begin JSON.parse config.public_manifest_path.read rescue JSON::ParserError puts "Webpacker JSON Parse error!" sleep 3 JSON.parse config.public_manifest_path.read end else {} end end end ruby

In my tests I now get "Webpacker JSON Parse error!" coming up occasionally.

Asked Oct 07 '21 01:10
avatar eadz

4 Answer:

Webpacker issue: https://github.com/rails/webpacker/issues/2324

Answered Oct 15 '19 at 01:53
avatar  of eadz

This issue has been automatically marked as stale because it has not been commented on for at least three months. The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the 6-0-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open. Thank you for all your contributions.

Answered Jan 13 '20 at 03:44
avatar  of rails-bot[bot]

I was also running into what I believe was a race condition in my tests that rely on Webpacker. Basically, I would call Webpacker.compile before the test suite to build assets and then execute the test cases (rspec capybara tests). But, I would keep getting back a return value of false from the #compile method, suggesting that compilation was not working. However, I could confirm via direct executions of bin/webpack that there wasn't actually anything wrong with the Webpack build itself. I also did not see this behaviour on my app's CI Pipeline.

I deduced that it was a race condition causing a problem because, while debugging the issue, I places a puts line directly into the Webpacker gem code. Suddenly, compilation before the test suite would work again. Did flushing the STDOUT stream with puts cause enough of a delay to workaround a race condition?

From looking at the stack trace above, I made a deduction that perhaps my JSON gem was too old....and lo and behold, it was a few years old. So, after upgrading json from 2.1.0 to 2.3.0 (and also webpacker from 4.0.7 to 4.2.2), the problem has gone away for good. So....maybe try upgrading old dependencies?

FWIW my project is still on Rails 5.2.

Answered Feb 12 '20 at 22:37
avatar  of ecbrodie

So, this is definitely a race condition in Webpacker. See https://github.com/rails/webpacker/issues/2860 for a correct workaround.

Answered Jan 04 '21 at 20:59
avatar  of Ecco