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

hs.ipc: Avoid reentrance when in printReplacement #3718

Merged
merged 2 commits into from
Nov 30, 2024

Conversation

dmgerman
Copy link

@dmgerman dmgerman commented Nov 28, 2024

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.

 LuaSkin: hs.ipc callback is being called recursively. Check your callback function, it is triggering further IPC messages. This message was triggered after reaching 5 recursive callbacks.

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.

hs.ipc replaces the print function with one that calls the original,
but also sends anything that print prints to any of the remote
connections. If you have debugging enabled, that means that the moment
the remote connection is created, a bunch of print statements are sent
to the this connection.

The problem I had is that my use of hs (command) was simply to send a
message to the screen:

     warning () {
        hs -c "hs.alert.show('$1')" > /dev/null
        echo "$1"
     }

The connection was so ephemeral, than I frequently got errors (related
to the client no long existing) which were tried to be send to the
client... so I ended with the following messages:

   2023-12-31 22:00:02: 22:00:02 ERROR: LuaSkin: hs.ipc callback is
   being called recursively. Check your callback function, it is
   triggering further IPC messages. This message was triggered after
   reaching 5 recursive callbacks.

This code avoid recursion for the same IPC client by keeping a counter
of how many times the client has entered the critical region.
Improved comments and identifiers of code that restricts
printReplacement to reenter when it is already inside a print
from a specific client.
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request does not contain a valid label. Please add one of the following labels: ['pr-fix', 'pr-change', 'pr-feature', 'pr-maintenance']

@dmgerman dmgerman marked this pull request as draft November 28, 2024 04:24
@dmgerman dmgerman marked this pull request as ready for review November 28, 2024 04:26
@cmsj cmsj added the pr-fix Pull Request implementing a bug fix label Nov 28, 2024
Copy link

codecov bot commented Nov 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 27.42%. Comparing base (37057a0) to head (5fc84ef).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3718      +/-   ##
==========================================
+ Coverage   27.37%   27.42%   +0.04%     
==========================================
  Files         191      191              
  Lines       51407    51407              
==========================================
+ Hits        14074    14098      +24     
+ Misses      37333    37309      -24     

@cmsj cmsj merged commit 931d38a into Hammerspoon:master Nov 30, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-fix Pull Request implementing a bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants