@@ -46,7 +46,7 @@ public interface ISyncSessionController
4646}
4747
4848// These values are the config option names used in the registry. Any option
49- // here can be configured with `(Debug)?Mutagen :OptionName` in the registry.
49+ // here can be configured with `(Debug)?MutagenController :OptionName` in the registry.
5050//
5151// They should not be changed without backwards compatibility considerations.
5252// If changed here, they should also be changed in the installer.
@@ -141,7 +141,7 @@ public async Task<List<SyncSession>> ListSyncSessions(CancellationToken ct)
141141 // reads of _sessionCount are atomic, so don't bother locking for this quick check.
142142 switch ( _sessionCount )
143143 {
144- case - 1 :
144+ case < 0 :
145145 throw new InvalidOperationException ( "Controller must be Initialized first" ) ;
146146 case 0 :
147147 // If we already know there are no sessions, don't start up the daemon
@@ -162,33 +162,14 @@ public async Task Initialize(CancellationToken ct)
162162 _sessionCount = - 2 ; // in progress
163163 }
164164
165- const int maxAttempts = 5 ;
166- ListResponse ? sessions = null ;
167- for ( var attempts = 1 ; attempts <= maxAttempts ; attempts ++ )
165+ var client = await EnsureDaemon ( ct ) ;
166+ var sessions = await client . Synchronization . ListAsync ( new ListRequest
168167 {
169- ct . ThrowIfCancellationRequested ( ) ;
170- try
168+ Selection = new Selection
171169 {
172- var client = await EnsureDaemon ( ct ) ;
173- sessions = await client . Synchronization . ListAsync ( new ListRequest
174- {
175- Selection = new Selection
176- {
177- All = true ,
178- } ,
179- } , cancellationToken : ct ) ;
180- }
181- catch ( Exception e ) when ( e is not OperationCanceledException )
182- {
183- if ( attempts == maxAttempts )
184- throw ;
185- // back off a little and try again.
186- await Task . Delay ( 100 , ct ) ;
187- continue ;
188- }
189-
190- break ;
191- }
170+ All = true ,
171+ } ,
172+ } , cancellationToken : ct ) ;
192173
193174 using ( _ = await _lock . LockAsync ( ct ) )
194175 {
@@ -211,7 +192,7 @@ public async Task Initialize(CancellationToken ct)
211192
212193 public async Task TerminateSyncSession ( SyncSession session , CancellationToken ct )
213194 {
214- if ( _sessionCount == - 1 ) throw new InvalidOperationException ( "Controller must be Initialized first" ) ;
195+ if ( _sessionCount < 0 ) throw new InvalidOperationException ( "Controller must be Initialized first" ) ;
215196 var client = await EnsureDaemon ( ct ) ;
216197 // TODO: implement
217198
@@ -272,11 +253,7 @@ private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
272253 // </summary>
273254 private void RemoveTransition ( Task < MutagenClient ? > transition )
274255 {
275- using ( _ = _lock . Lock ( ) )
276- {
277- ;
278- }
279-
256+ using var _ = _lock . Lock ( ) ;
280257 if ( _inProgressTransition == transition ) _inProgressTransition = null ;
281258 }
282259
@@ -293,9 +270,31 @@ private void RemoveTransition(Task<MutagenClient?> transition)
293270 // Mainline; no daemon running.
294271 }
295272
296- using ( _ = await _lock . LockAsync ( ct ) )
273+ // If we get some failure while creating the log file or starting the process, we'll retry
274+ // it up to 5 times x 100ms. Those issues should resolve themselves quickly if they are
275+ // going to at all.
276+ const int maxAttempts = 5 ;
277+ ListResponse ? sessions = null ;
278+ for ( var attempts = 1 ; attempts <= maxAttempts ; attempts ++ )
297279 {
298- StartDaemonProcessLocked ( ) ;
280+ ct . ThrowIfCancellationRequested ( ) ;
281+ try
282+ {
283+ using ( _ = await _lock . LockAsync ( ct ) )
284+ {
285+ StartDaemonProcessLocked ( ) ;
286+ }
287+ }
288+ catch ( Exception e ) when ( e is not OperationCanceledException )
289+ {
290+ if ( attempts == maxAttempts )
291+ throw ;
292+ // back off a little and try again.
293+ await Task . Delay ( 100 , ct ) ;
294+ continue ;
295+ }
296+
297+ break ;
299298 }
300299
301300 return await WaitForDaemon ( ct ) ;
0 commit comments