Skip to content

Commit

Permalink
Lock mutex when reading isAuthorizedCalls map
Browse files Browse the repository at this point in the history
  • Loading branch information
denisonbarbosa committed Aug 11, 2023
1 parent af29675 commit 1c864aa
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/testutils/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type isAuthorizedCtx struct {
type BrokerBusMock struct {
name string
isAuthorizedCalls map[string]isAuthorizedCtx
isAuthorizedCallsMu sync.Mutex
isAuthorizedCallsMu sync.RWMutex
}

// StartBusBrokerMock starts the D-Bus service and exports it on the system bus.
Expand All @@ -57,7 +57,7 @@ func StartBusBrokerMock(t *testing.T) string {
bus := BrokerBusMock{
name: brokerName,
isAuthorizedCalls: map[string]isAuthorizedCtx{},
isAuthorizedCallsMu: sync.Mutex{},
isAuthorizedCallsMu: sync.RWMutex{},
}
err = conn.Export(&bus, dbus.ObjectPath(busObjectPath), busInterface)
require.NoError(t, err, "Setup: could not export mock broker")
Expand Down Expand Up @@ -216,9 +216,13 @@ func (b *BrokerBusMock) IsAuthorized(sessionID, authenticationData string) (acce
}

// Handles the context that will be assigned for the IsAuthorized handler
b.isAuthorizedCallsMu.RLock()
if _, exists := b.isAuthorizedCalls[sessionID]; exists {
b.isAuthorizedCallsMu.RUnlock()
return "", "", dbus.MakeFailedError(fmt.Errorf("Broker %q: IsAuthorized already running for session %q", b.name, sessionID))
}
b.isAuthorizedCallsMu.RUnlock()

ctx, cancel := context.WithCancel(context.Background())
b.isAuthorizedCallsMu.Lock()
b.isAuthorizedCalls[sessionID] = isAuthorizedCtx{ctx, cancel}
Expand Down

0 comments on commit 1c864aa

Please sign in to comment.