Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSON validator co-process error handling #15

Merged
merged 8 commits into from
Jul 23, 2024
Merged

Conversation

h4l
Copy link
Owner

@h4l h4l commented Jul 22, 2024

This PR hardens the interaction with the JSON validator co-process, to avoid the error seen in #10 with _PID not being set after starting a coproc.

Related to this, we also now support using the JSON_BASH_GREP envar to change the grep command to a custom path, and we default to ggrep if it's available.

This resolves #12.

h4l added 2 commits July 20, 2024 13:05
I believe this was used to strip whitespace before the regex was
pre-generated in hack/syntax_patterns.bash.
@h4l h4l mentioned this pull request Jul 22, 2024
Closed
h4l added 6 commits July 23, 2024 04:56
The json validator grep coproc interaction was working reliably when
grep was GNU grep, but it was not gracefully handling the termination of
the coproc in all cases. When a coproc exits immediately at startup,
accessing the coproc special variables (json_validator and
json_validator_PID) is subject to race condition, because bash unsets
the variables as soon as the coproc process exits. In the previous
implementation, we read the PID with ${:?} syntax, which could fail if
the PID var was unset by bash.

We now interact with the coproc in a rather more defensive way, handling
either or both of json_validator and json_validator_PID being unset at
creation; and then also handling SIGPIPE errors when later writing to
the FD of our writable end of the coproc's stdin. This is somewhat
fiddly, as it requires trapping SIGPIPE errors, otherwise bash exits
with 141.

We now have tests covering coproc failure at startup, after startup, and
IO read & write errors. We can also restart the coproc after an IO
error to recover. This could potentially occur when a read times out
under high system load.

Finally, several local vars in json.validate were not prefixed, so they
could collide with a caller's in= reference var. They're all prefixed
now.
We do this in json.validate now.
The JSON_BASH_GREP envar now defines the grep command used by json.bash
when it starts a json validator co-process. The envar can be set to a :
delimited list of paths/commands to use. It defaults to "ggrep:grep", so
systems with ggrep installed with non-GNU grep will use ggrep.
@h4l h4l merged commit ee1c6be into main Jul 23, 2024
17 checks passed
@h4l h4l deleted the handle-bad-grep branch July 23, 2024 05:09
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.

Behaviour when grep is not GNU grep is not user-friendly & in some cases fails to trigger stream poisoning
1 participant