Make hook context input reads thread-safe#882
Open
Felix-LeeSM wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aspre-push, one hook can consume the stream while another reads EOF and caches an empty string.If that happens, helpers like
pushed_refscan silently see no refs even though Git did provide them.Changes
input_stringread so the stream is consumed once.This follows up on #881.
Verification
Focused checks pass locally:
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.