Skip to content

Commit

Permalink
server/tailsql: swap database handles outside the server lock
Browse files Browse the repository at this point in the history
The server needs to lock access to the list of handles, but the swap into an
existing handle does not depend on server state. Ensure we do the swap outside
the server lock so a slow query (which delays the handle swap) does not block
the UI while it runs.
  • Loading branch information
creachadair committed Oct 3, 2023
1 parent 1023ec2 commit 930ccbe
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions server/tailsql/tailsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,17 @@ func (s *Server) SetDB(source string, db *sql.DB, opts *DBOptions) bool {
panic("new database is nil")
}
s.mu.Lock()
defer s.mu.Unlock()

for _, src := range s.dbs {
if src.Source() == source {
s.mu.Unlock()

// Perform the swap outside the service lock, since it may wait if a
// query is in-flight and we don't need or want to block the rest of
// the UI while that's happening.
old := src.swap(db, opts)
old.Close()
return true
return false
}
}
s.dbs = append(s.dbs, &dbHandle{
Expand All @@ -199,6 +203,7 @@ func (s *Server) SetDB(source string, db *sql.DB, opts *DBOptions) bool {
label: opts.label(),
named: opts.namedQueries(),
})
s.mu.Unlock()
return false
}

Expand Down

0 comments on commit 930ccbe

Please sign in to comment.