@@ -223,12 +223,8 @@ func (s *Store) RecreateAll(_ context.Context) error {
223223 return err
224224 }
225225
226- // We repeatedly lock and unlock the mutex in this function to give other threads a chance to talk to the credential store.
227- // It can take several minutes to recreate the credentials if there are hundreds of them, and we don't want to
228- // block all other threads while we do that.
229226 // New credentials might be created after our GetAll, but they will be created with the current encryption configuration,
230227 // so it's okay that they are skipped by this function.
231-
232228 s .recreateAllLock .Lock ()
233229 all , err := store .GetAll ()
234230 s .recreateAllLock .Unlock ()
@@ -238,28 +234,33 @@ func (s *Store) RecreateAll(_ context.Context) error {
238234
239235 // Loop through and recreate each individual credential.
240236 for serverAddress := range all {
241- s .recreateAllLock .Lock ()
242- authConfig , err := store .Get (serverAddress )
243- if err != nil {
244- s .recreateAllLock .Unlock ()
245-
246- if IsCredentialsNotFoundError (err ) {
247- // This can happen if the credential was deleted between the GetAll and the Get by another thread.
248- continue
249- }
237+ if err := s .recreateCredential (store , serverAddress ); err != nil {
250238 return err
251239 }
240+ }
252241
253- if err := store .Erase (serverAddress ); err != nil {
254- s .recreateAllLock .Unlock ()
255- return err
256- }
242+ return nil
243+ }
257244
258- if err := store .Store (authConfig ); err != nil {
259- s .recreateAllLock .Unlock ()
260- return err
245+ func (s * Store ) recreateCredential (store credentials.Store , serverAddress string ) error {
246+ s .recreateAllLock .Lock ()
247+ defer s .recreateAllLock .Unlock ()
248+
249+ authConfig , err := store .Get (serverAddress )
250+ if err != nil {
251+ if IsCredentialsNotFoundError (err ) {
252+ // This can happen if the credential was deleted between the GetAll and the Get by another thread.
253+ return nil
261254 }
262- s .recreateAllLock .Unlock ()
255+ return err
256+ }
257+
258+ if err := store .Erase (serverAddress ); err != nil {
259+ return err
260+ }
261+
262+ if err := store .Store (authConfig ); err != nil {
263+ return err
263264 }
264265
265266 return nil
0 commit comments