From 70fd443c254c78f78c9585728f2c03b0cd97e8bb Mon Sep 17 00:00:00 2001 From: William Martin Date: Thu, 14 Mar 2024 20:41:01 +0100 Subject: [PATCH] Fix race condition in prompt handling The current approach to handling prompts involves calling for a prompt and then registering a signal handler for a response that indicates the prompt had completed. In most cases, if the user were to take action, this would be quite slow. However, in the case of automatic responses to the prompt, the signal response may appear before the signal handler is configured, resulting in blocking forever. This commit moves the call for a prompt after the signal handling has been configured, closing the race window. Signed-off-by: William Martin --- secret_service/secret_service.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/secret_service/secret_service.go b/secret_service/secret_service.go index 07be546..c346271 100644 --- a/secret_service/secret_service.go +++ b/secret_service/secret_service.go @@ -186,12 +186,7 @@ func (s *SecretService) CreateItem(collection dbus.BusObject, label string, attr // the prompt to the user. func (s *SecretService) handlePrompt(prompt dbus.ObjectPath) (bool, dbus.Variant, error) { if prompt != dbus.ObjectPath("/") { - err := s.Object(serviceName, prompt).Call(promptInterface+".Prompt", 0, "").Err - if err != nil { - return false, dbus.MakeVariant(""), err - } - - err = s.AddMatchSignal(dbus.WithMatchObjectPath(prompt), + err := s.AddMatchSignal(dbus.WithMatchObjectPath(prompt), dbus.WithMatchInterface(promptInterface), ) if err != nil { @@ -205,6 +200,11 @@ func (s *SecretService) handlePrompt(prompt dbus.ObjectPath) (bool, dbus.Variant promptSignal := make(chan *dbus.Signal, 1) s.Signal(promptSignal) + err = s.Object(serviceName, prompt).Call(promptInterface+".Prompt", 0, "").Err + if err != nil { + return false, dbus.MakeVariant(""), err + } + signal := <-promptSignal switch signal.Name { case promptInterface + ".Completed":