security: do not silence Connect() errors; read values and use under the same lock.

Ignoring Connect() errors can sometimes cause double-registration.
Reading values under one lock and using them under another allows for values to change in the gap between them.
This commit is contained in:
Jay
2026-08-09 18:58:13 -04:00
parent 8ed9efa433
commit 7e54a26aac
+7 -6
View File
@@ -143,17 +143,18 @@ func (e *Embassy) Dispatch(url string) error {
return fmt.Errorf("invalid url: %w", err) return fmt.Errorf("invalid url: %w", err)
} }
e.mu.RLock() e.mu.Lock()
_, exists := e.envoys[url] _, exists := e.envoys[url]
if exists { if exists {
e.mu.RUnlock() e.mu.Unlock()
return fmt.Errorf("already dispatched: %s", url) return fmt.Errorf("already dispatched: %s", url)
} }
e.mu.RUnlock()
e.mu.Lock() err = e.pool.Connect(url)
if err != nil {
e.pool.Connect(url) e.mu.Unlock()
return err
}
terminate := func() { e.dismiss(url) } terminate := func() { e.dismiss(url) }
send := func(data []byte) error { return e.send(url, data) } send := func(data []byte) error { return e.send(url, data) }