@@ -67,7 +67,7 @@ public interface ISignalLibHandle
67
67
Task Reacquire ( ) ;
68
68
void Release ( ) ;
69
69
bool AddFrontend ( CoreDispatcher d , ISignalFrontend w ) ;
70
- void RemoveFrontend ( CoreDispatcher d ) ;
70
+ Task RemoveFrontend ( CoreDispatcher d ) ;
71
71
72
72
// Background API
73
73
event EventHandler < SignalMessageEventArgs > SignalMessageEvent ;
@@ -144,20 +144,25 @@ public bool AddFrontend(CoreDispatcher d, ISignalFrontend w)
144
144
}
145
145
}
146
146
147
- public void RemoveFrontend ( CoreDispatcher d )
147
+ public async Task RemoveFrontend ( CoreDispatcher d )
148
148
{
149
149
Logger . LogTrace ( "RemoveFrontend() locking" ) ;
150
- if ( SynchronizationContext . Current != null )
150
+ await SemaphoreSlim . WaitAsync ( CancelSource . Token ) ;
151
+ try
151
152
{
152
- Logger . LogCritical ( "RemoveFrontend must not be called with a syncchronization context" ) ;
153
- throw new InvalidOperationException ( ) ;
153
+ Logger . LogTrace ( "RemoveFrontend() locked" ) ;
154
+ Logger . LogInformation ( "Unregistering frontend of dispatcher {0}" , d . GetHashCode ( ) ) ;
155
+ Frames . Remove ( d ) ;
156
+ }
157
+ catch ( Exception e )
158
+ {
159
+ Logger . LogCritical ( $ "RemoveFrontend failed(): { e . Message } ({ e . GetType ( ) } )\n { e . StackTrace } ") ;
160
+ }
161
+ finally
162
+ {
163
+ SemaphoreSlim . Release ( ) ;
164
+ Logger . LogTrace ( "RemoveFrontend() released" ) ;
154
165
}
155
- SemaphoreSlim . Wait ( CancelSource . Token ) ;
156
- Logger . LogTrace ( "RemoveFrontend() locked" ) ;
157
- Logger . LogInformation ( "Unregistering frontend of dispatcher {0}" , d . GetHashCode ( ) ) ;
158
- Frames . Remove ( d ) ;
159
- SemaphoreSlim . Release ( ) ;
160
- Logger . LogTrace ( "RemoveFrontend() released" ) ;
161
166
}
162
167
163
168
public void PurgeAccountData ( )
@@ -245,11 +250,13 @@ public async Task Reacquire()
245
250
GlobalResetEvent . Reset ( ) ;
246
251
LibsignalDBContext . ClearSessionCache ( ) ;
247
252
Instance = this ;
253
+ Logger . LogTrace ( $ "Reacquire() updating { Frames . Count } frames") ;
248
254
await Task . Run ( async ( ) =>
249
255
{
250
256
List < Task > tasks = new List < Task > ( ) ;
251
257
foreach ( var f in Frames )
252
258
{
259
+ Logger . LogTrace ( $ "Reacquire() updating frame { f . Value } ") ;
253
260
var conversations = GetConversations ( ) ;
254
261
var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
255
262
await f . Key . RunAsync ( CoreDispatcherPriority . Normal , ( ) =>
@@ -273,6 +280,7 @@ await f.Key.RunAsync(CoreDispatcherPriority.Normal, () =>
273
280
{
274
281
await t ;
275
282
}
283
+ Logger . LogTrace ( $ "Reacquire() recovering downloads") ;
276
284
await RecoverDownloads ( ) ;
277
285
Store = LibsignalDBContext . GetSignalStore ( ) ;
278
286
if ( Store != null )
@@ -282,6 +290,7 @@ await f.Key.RunAsync(CoreDispatcherPriority.Normal, () =>
282
290
} ) ;
283
291
if ( LikelyHasValidStore )
284
292
{
293
+ Logger . LogTrace ( $ "Reacquire() initializing network") ;
285
294
InitNetwork ( ) ;
286
295
}
287
296
}
0 commit comments