@@ -302,6 +302,16 @@ func (s *mocks) GetHistoryByPath(sessionID, filterPath string) (types.History, e
302302}
303303
304304func (s * mocks ) NewSession (name string ) * types.Session {
305+ s .mu .Lock ()
306+ defer s .mu .Unlock ()
307+ return s .newSessionLocked (name )
308+ }
309+
310+ // newSessionLocked appends a new session and returns it. The caller MUST hold s.mu: the default
311+ // name and the carried-over locked mocks are both derived from s.sessions, so the whole thing has
312+ // to happen in one critical section — otherwise two concurrent creators can read the same length
313+ // and both mint e.g. "Session #1" (the bug behind the flaky rename e2e test).
314+ func (s * mocks ) newSessionLocked (name string ) * types.Session {
305315 if strings .TrimSpace (name ) == "" {
306316 name = fmt .Sprintf ("Session #%d" , len (s .sessions )+ 1 )
307317 }
@@ -313,21 +323,16 @@ func (s *mocks) NewSession(name string) *types.Session {
313323 history = types.History {}
314324 }
315325
326+ // Carry over the locked mocks from the current last session, reset for the new one.
316327 mocks := types.Mocks {}
317328 if len (s .sessions ) > 0 {
318- session := s .GetLastSession ()
319- s .mu .Lock ()
320- for _ , mock := range session .Mocks {
329+ for _ , mock := range s .sessions [len (s .sessions )- 1 ].Mocks {
321330 if mock .State .Locked {
322331 mocks = append (mocks , mock .CloneAndReset ())
323332 }
324333 }
325- s .mu .Unlock ()
326334 }
327335
328- s .mu .Lock ()
329- defer s .mu .Unlock ()
330-
331336 session := & types.Session {
332337 ID : types .NewID (),
333338 Name : name ,
@@ -384,12 +389,10 @@ func (s *mocks) DeleteSession(id string) error {
384389
385390func (s * mocks ) GetLastSession () * types.Session {
386391 s .mu .Lock ()
392+ defer s .mu .Unlock ()
387393 if len (s .sessions ) == 0 {
388- s .mu .Unlock ()
389- s .NewSession ("" )
390- s .mu .Lock ()
394+ return s .newSessionLocked ("" ).Clone ()
391395 }
392- defer s .mu .Unlock ()
393396 return s .sessions [len (s .sessions )- 1 ].Clone ()
394397}
395398
0 commit comments