hs.ipc: Avoid reentrance when in printReplacement #3718
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.
when hs.ipc is enabled, print is replaced with printReplacement. The idea is to make sure that prints are sent to both the console and the client.
The problem arises when the client stops responding (either it is dead or it is taking time to respond). In that case, an error is generated, and a print is issued with the error/warning. Thus printReplacement is called again. This can go until HS detects the recursion and stops it. In some cases, this might cause crashes.
See this thread for context: https://groups.google.com/g/hammerspoon/c/dMH-exXe6ng
This code restricts printReplacement to never send a message while it is inside already of printReplacement for that particular client ID.
It is implemented by using a table indexed by the client ID. To avoid the table to grow forever, once a client ID counter is zero, it is removed from the table.