Skip to content

fix: Turbo DriveのDOM差し替え中に起きるsystem specの不安定な失敗を解消する - #219

Merged
Tsuchiya2 merged 1 commit into
mainfrom
fix/flaky-system-test-turbo-dom-swap
Aug 24, 2026
Merged

fix: Turbo DriveのDOM差し替え中に起きるsystem specの不安定な失敗を解消する#219
Tsuchiya2 merged 1 commit into
mainfrom
fix/flaky-system-test-turbo-dom-swap

Conversation

@Tsuchiya2

Copy link
Copy Markdown
Owner

背景

mainのCIでsystem specがまれに落ちていた。

1) [SystemTest] AlarmContents 新規アラームコンテンツ作成 ...
   Failure/Error: expect(page).to have_content('アラームコンテンツ一覧')

   Selenium::WebDriver::Error::UnknownError:
     unknown error: unhandled inspector error:
     {"code":-32000,"message":"Node with given id does not belong to the document"}

原因

フォーム送信後のリダイレクトをTurbo Driveが処理する際、bodyの差し替えが終わる前にCapybaraがhave_contentの問い合わせを投げると、chromedriverが参照していたノードが旧ドキュメント側に取り残されてこのエラーになる。

差し替えが終われば解消する一過性のエラーだが、Capybaraが再試行対象とするinvalid_element_errorsStaleElementReferenceErrorなど)にUnknownErrorは含まれないため、待機時間を使い切ることなく即失敗していた。

対応

Capybara::Node::Base#catch_error?をprependで拡張し、このメッセージを持つUnknownErrorに限り再試行対象として扱うようにした。

  • 待機時間(Capybara.default_max_wait_time)内に差し替えが完了すれば成功する
  • 超過すれば元の例外がそのまま送出されるため、本物のエラーを握り潰すことはない
  • メッセージで絞っているので、他のUnknownErrorは従来どおり即失敗する

specごとにsleepや明示的なwaitを足す案は採らなかった。同じ競合はTurboで遷移するすべてのsystem specで起こりうるため、発生箇所ごとの対処では漏れが出るため。

確認したこと

  • spec/system全件(62 examples, 0 failures, 6 pending)
  • 対象メッセージのUnknownErrorは再試行され、それ以外のUnknownErrorは再試行されないことを検証
  • Rubocop: no offenses

CIでalarm_contents_specがまれに以下で落ちていた。

  Selenium::WebDriver::Error::UnknownError:
    unknown error: unhandled inspector error:
    {"code":-32000,"message":"Node with given id does not belong to the document"}

フォーム送信後のリダイレクトをTurbo Driveが処理する際、bodyの差し替えが
終わる前にCapybaraがhave_contentの問い合わせを投げると、chromedriverが
参照していたノードが旧ドキュメント側に取り残されてこのエラーになる。
差し替えが終われば解消する一過性のものだが、Capybaraが再試行する
invalid_element_errorsにUnknownErrorは含まれないため、待機時間を使い切る
ことなく即失敗していた。

そこでCapybara::Node::Base#catch_error?をprependで拡張し、このメッセージを
持つUnknownErrorに限りStaleElementReferenceErrorなどと同じ再試行対象として
扱うようにした。待機時間内に差し替えが完了すれば成功し、超過すれば元の例外が
そのまま送出されるので、本物のエラーを握り潰すことはない。

specごとにsleepやwaitを足す案は採らなかった。同じ競合はTurboで遷移する
すべてのsystem specで起こりうるため、発生箇所ごとの対処では漏れが出る。
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 31 minutes.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8923be4e-1420-4e32-82dc-4f36329ee67c

📥 Commits

Reviewing files that changed from the base of the PR and between 02ec30f and 5d14d7d.

📒 Files selected for processing (1)
  • spec/support/capybara.rb

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Tsuchiya2
Tsuchiya2 merged commit 91d287b into main Aug 24, 2026
3 checks passed
@Tsuchiya2
Tsuchiya2 deleted the fix/flaky-system-test-turbo-dom-swap branch August 24, 2026 08:42
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.

1 participant