Skip to content

Commit

Permalink
Fix race condition in prompt handling
Browse files Browse the repository at this point in the history
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 <williammartin@github.com>
  • Loading branch information
williammartin committed Mar 15, 2024
1 parent b0e756d commit 70fd443
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions secret_service/secret_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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":
Expand Down

0 comments on commit 70fd443

Please sign in to comment.