Skip to content

Make hook context input reads thread-safe#882

Open
Felix-LeeSM wants to merge 1 commit into
sds:mainfrom
Felix-LeeSM:fix/thread-safe-hook-input
Open

Make hook context input reads thread-safe#882
Felix-LeeSM wants to merge 1 commit into
sds:mainfrom
Felix-LeeSM:fix/thread-safe-hook-input

Conversation

@Felix-LeeSM
Copy link
Copy Markdown

Summary

This fixes a small race in HookContext::Base#input_string.

Hook contexts currently cache stdin lazily with @input_string ||= @input.read. Since hooks are parallelized by default, two hooks sharing the same context can race during that first read. For hook types that receive real data on stdin, such as pre-push, one hook can consume the stream while another reads EOF and caches an empty string.

If that happens, helpers like pushed_refs can silently see no refs even though Git did provide them.

Changes

  • Add a mutex around the first input_string read so the stream is consumed once.
  • Keep the cached string shared by all callers after the initial read.
  • Add a spec that forces a concurrent read attempt without relying on flaky timing.

This follows up on #881.

Verification

Focused checks pass locally:

bundle exec rspec spec/overcommit/hook_context/base_spec.rb
bundle exec rubocop lib/overcommit/hook_context/base.rb spec/overcommit/hook_context/base_spec.rb
bundle exec overcommit --run

I also ran the full spec suite. It still has a few unrelated failures around git alias amendment detection on my machine, but this change's focused spec passes.

Hook contexts cache stdin lazily through input_string. Since hooks run in parallel by default, two hooks can race during that first read from the shared input stream.

That is especially visible in pre-push hooks, where Git provides refs on stdin. One thread can consume the refs, while another reads EOF and caches an empty string. If that happens, helpers like pushed_refs can silently see no refs.

Use a mutex around the first read so the stream is consumed once and every caller gets the same cached input.

Fixes sds#881
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