Skip to content

lima-guestagent: support external trigger via sighup notification #3766

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ciderale
Copy link

The Lima guest agent currently uses a polling strategy to update the ports exposed to the host system. For some use cases (e.g. testcontainers (rancher-sandbox/rancher-desktop#3141)), this may not be responsive enough. This PR supplements the periodic ticker with an optional, push-based trigger based on OS signals. External processes can thus trigger a port update via SIGHUP, enabling a more timely response -- for example, by reacting to docker events.

In my testing, this consistently reduced the time until a port became reachable -- without increasing the polling frequency.
The implementation requires minimal changes to lima-guestagent and is not tied to a specific event source. i.e. it is open to any kind of trigger.

I hope this change is of value to this interesting project. I'm looking forward to your feedback.

Validation Experiment

An example implementation of the trigger might look like:

docker events | while IFS= read -r _; do kill -HUP "$AGENT_PID"; done

which can be run in a systemd unit and optionally filter for specific events (e.g. start/stop/kill).

Timing experiment has been done with ciderale/nixos-lima using lima 1.2.0 with test-portmapping.sh, starting 100 alpine/socat containers (each delayed by 0.3s, on different ports) and measuring time till each request has been fulfilled. The results are:

  • Without the docker events SIGHUP trigger (i.e. the normal periodic update with default every 3s): the measured time is uniformly distributed between 240ms and 3160ms. As expected, the average is about half the polling frequency.
  • With the proposed, additional SIGHUP trigger: the port is consistently available between 150ms to 220ms.

Supplement the periodic port update ticker by a signal based trigger
mechanism. This allows for more responsive update of the portmapping.
External tools can notify the lima-guestagent by means of a SIGHUP.
The SIGHUP notification is handled like the periodic update ticker.

Signed-off-by: Alain Lehmann <alain.lehmann@gmail.com>
@jandubois
Copy link
Member

Thank you for your PR!

There is however a more comprehensive PR in preparation, that will use the events API for containerd, dockerd, and kubernetes, to send immediate port notifications. For various non-technical reasons it has been in progress for a long time, but should be ready for consideration very soon. It is based on the Rancher Desktop guest agent on Windows. You can take a look at the in-progress changes at Nino-K#2

Given that this PR will hopefully land within the next couple of weeks, I think this signal mechanism should not be merged.

@ciderale
Copy link
Author

Thank you for the quick response. I see that the referenced PR is more tightly integrated (at the cost of more complexity). The openness of the signal based approach might still be interesting, though. But I see that most use cases (including my current one) are probably covered by that other PR. So I'm looking forward to that other PR to land. Thanks again.

@@ -58,7 +60,43 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
// without depending on `bpftrace` binary.
// The agent binary will need CAP_BPF file cap.
ticker := time.NewTicker(tick)
return ticker.C, ticker.Stop

logrus.Info("register for sighup notifications")
Copy link
Member

Choose a reason for hiding this comment

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

Nit: probably sighup should be spelled in the upper chars, as it might look like "sigh up" (?) or a typo of sign-up


logrus.Info("register for sighup notifications")
sighupC := make(chan os.Signal, 1)
signal.Notify(sighupC, syscall.SIGHUP)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment line to explain who sends SIGHUP

Copy link
Author

Choose a reason for hiding this comment

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

Sure, I have added a comment about the purpose of the SIGHUP mechanism and also improved the log message.

Signed-off-by: Alain Lehmann <alain.lehmann@gmail.com>
@AkihiroSuda AkihiroSuda requested a review from a team August 5, 2025 05:33
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.

3 participants