Skip to content

Commit bd6cdac

Browse files
henrybarretogustavosbarreto
authored andcommitted
fix(ssh): avoid potencial concurrency problems when creating and updating seats
1 parent eb53c65 commit bd6cdac

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

ssh/session/session.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ type Seat struct {
170170
}
171171

172172
type Seats struct {
173+
mu *sync.Mutex
173174
// counter count atomically seats of a session.
174175
counter *atomic.Int32
175176
// Items represents the individual seat of a session.
@@ -179,13 +180,17 @@ type Seats struct {
179180
// NewSeats creates a new [Seats] defining initial values for internal properties.
180181
func NewSeats() Seats {
181182
return Seats{
183+
mu: new(sync.Mutex),
182184
counter: new(atomic.Int32),
183185
Items: new(sync.Map),
184186
}
185187
}
186188

187189
// NewSeat creates a new seat inside seats.
188190
func (s *Seats) NewSeat() (int, error) {
191+
s.mu.Lock()
192+
defer s.mu.Unlock()
193+
189194
id := int(s.counter.Load())
190195
defer s.counter.Add(1)
191196

@@ -213,6 +218,9 @@ func (s *Seats) Get(seat int) (*Seat, bool) {
213218

214219
// SetPty sets a pty status to a seat from their id.
215220
func (s *Seats) SetPty(seat int, status bool) {
221+
s.mu.Lock()
222+
defer s.mu.Unlock()
223+
216224
item, ok := s.Get(seat)
217225
if !ok {
218226
log.Warn("failed to set pty because no seat was created before")

0 commit comments

Comments
 (0)