1+ using System . Diagnostics ;
12using System . Text ;
23using UniGetUI . Core . Logging ;
34using UniGetUI . Core . SettingsEngine ;
@@ -263,6 +264,67 @@ public bool IsReady()
263264 return _ready && IsEnabled ( ) && Status . Found ;
264265 }
265266
267+ // Opt out (override => false) when AttemptFastRepair can't recover a timed-out query (issue #4974).
268+ protected virtual bool RetryListingTasksOnTimeout => true ;
269+
270+ // Processes started by the current listing task, so a timeout can kill them instead of orphaning them.
271+ private readonly AsyncLocal < List < Process > ? > _listingProcesses = new ( ) ;
272+
273+ // Lets a listing task register a process for kill-on-timeout. No-op when outside a listing task.
274+ protected void RegisterListingProcess ( Process process )
275+ {
276+ List < Process > ? processes = _listingProcesses . Value ;
277+ if ( processes is null )
278+ return ;
279+ lock ( processes )
280+ processes . Add ( process ) ;
281+ }
282+
283+ private static void KillListingProcesses ( List < Process > processes )
284+ {
285+ lock ( processes )
286+ foreach ( Process p in processes )
287+ {
288+ try
289+ {
290+ if ( ! p . HasExited )
291+ p . Kill ( entireProcessTree : true ) ;
292+ }
293+ catch ( Exception ex )
294+ {
295+ Logger . Warn ( $ "Could not kill a timed-out process tree: { ex . Message } ") ;
296+ }
297+ }
298+ }
299+
300+ private T RunListingTaskWithTimeout < T > ( Func < T > method , string taskName )
301+ {
302+ List < Process > processes = [ ] ;
303+ var task = Task . Run ( ( ) =>
304+ {
305+ _listingProcesses . Value = processes ;
306+ return method ( ) ;
307+ } ) ;
308+
309+ if ( ! task . Wait ( TimeSpan . FromSeconds ( PackageListingTaskTimeout ) ) )
310+ {
311+ if ( ! Settings . Get ( Settings . K . DisableTimeoutOnPackageListingTasks ) )
312+ {
313+ KillListingProcesses ( processes ) ;
314+ CoreTools . FinalizeDangerousTask ( task ) ;
315+ throw new TimeoutException (
316+ $ "Task { taskName } for manager { Name } did not finish after "
317+ + $ "{ PackageListingTaskTimeout } seconds, aborting. You may disable "
318+ + $ "timeouts from UniGetUI Advanced Settings"
319+ ) ;
320+ }
321+
322+ task . Wait ( ) ;
323+ }
324+
325+ return task . GetAwaiter ( ) . GetResult ( ) ;
326+ }
327+
266328 /// <summary>
267329 /// Returns an array of Package objects that the manager lists for the given query. Depending on the manager, the list may
268330 /// also include similar results. This method is fail-safe and will return an empty array if an error occurs.
@@ -278,36 +340,22 @@ private IReadOnlyList<IPackage> _findPackages(string query, bool SecondAttempt)
278340 }
279341 try
280342 {
281- var task = Task . Run ( ( ) => FindPackages_UnSafe ( query ) ) ;
282- if ( ! task . Wait ( TimeSpan . FromSeconds ( PackageListingTaskTimeout ) ) )
283- {
284- if ( ! Settings . Get ( Settings . K . DisableTimeoutOnPackageListingTasks ) )
285- {
286- CoreTools . FinalizeDangerousTask ( task ) ;
287- throw new TimeoutException (
288- $ "Task _getInstalledPackages for manager { Name } did not finish after "
289- + $ "{ PackageListingTaskTimeout } seconds, aborting. You may disable "
290- + $ "timeouts from UniGetUI Advanced Settings"
291- ) ;
292- }
293-
294- task . Wait ( ) ;
295- }
296-
297- var packages = task . GetAwaiter ( ) . GetResult ( ) ;
343+ var packages = RunListingTaskWithTimeout (
344+ ( ) => FindPackages_UnSafe ( query ) ,
345+ "_findPackages"
346+ ) ;
298347 Logger . Info (
299348 $ "Found { packages . Count } available packages from { Name } with the query { query } "
300349 ) ;
301350 return packages ;
302351 }
303352 catch ( Exception e )
304353 {
305- if ( ! SecondAttempt )
354+ while ( e is AggregateException )
355+ e = e . InnerException ?? new InvalidOperationException ( "How did we get here?" ) ;
356+
357+ if ( ! SecondAttempt && ( RetryListingTasksOnTimeout || e is not TimeoutException ) )
306358 {
307- while ( e is AggregateException )
308- e =
309- e . InnerException
310- ?? new InvalidOperationException ( "How did we get here?" ) ;
311359 Logger . Warn (
312360 $ "Manager { DisplayName } failed to find packages with exception { e . GetType ( ) . Name } : { e . Message } "
313361 ) ;
@@ -342,34 +390,20 @@ private IReadOnlyList<IPackage> _getAvailableUpdates(bool SecondAttempt)
342390 Task . Run ( RefreshPackageIndexes )
343391 . Wait ( TimeSpan . FromSeconds ( PackageListingTaskTimeout ) ) ;
344392
345- var task = Task . Run ( GetAvailableUpdates_UnSafe ) ;
346- if ( ! task . Wait ( TimeSpan . FromSeconds ( PackageListingTaskTimeout ) ) )
347- {
348- if ( ! Settings . Get ( Settings . K . DisableTimeoutOnPackageListingTasks ) )
349- {
350- CoreTools . FinalizeDangerousTask ( task ) ;
351- throw new TimeoutException (
352- $ "Task _getInstalledPackages for manager { Name } did not finish after "
353- + $ "{ PackageListingTaskTimeout } seconds, aborting. You may disable "
354- + $ "timeouts from UniGetUI Advanced Settings"
355- ) ;
356- }
357-
358- task . Wait ( ) ;
359- }
360-
361- var packages = task . GetAwaiter ( ) . GetResult ( ) ;
393+ var packages = RunListingTaskWithTimeout (
394+ GetAvailableUpdates_UnSafe ,
395+ "_getAvailableUpdates"
396+ ) ;
362397 Logger . Info ( $ "Found { packages . Count } available updates from { Name } ") ;
363398 return packages ;
364399 }
365400 catch ( Exception e )
366401 {
367- if ( ! SecondAttempt )
402+ while ( e is AggregateException )
403+ e = e . InnerException ?? new InvalidOperationException ( "How did we get here?" ) ;
404+
405+ if ( ! SecondAttempt && ( RetryListingTasksOnTimeout || e is not TimeoutException ) )
368406 {
369- while ( e is AggregateException )
370- e =
371- e . InnerException
372- ?? new InvalidOperationException ( "How did we get here?" ) ;
373407 Logger . Warn (
374408 $ "Manager { DisplayName } failed to list available updates with exception { e . GetType ( ) . Name } : { e . Message } "
375409 ) ;
@@ -401,34 +435,20 @@ private IReadOnlyList<IPackage> _getInstalledPackages(bool SecondAttempt)
401435 }
402436 try
403437 {
404- var task = Task . Run ( GetInstalledPackages_UnSafe ) ;
405- if ( ! task . Wait ( TimeSpan . FromSeconds ( PackageListingTaskTimeout ) ) )
406- {
407- if ( ! Settings . Get ( Settings . K . DisableTimeoutOnPackageListingTasks ) )
408- {
409- CoreTools . FinalizeDangerousTask ( task ) ;
410- throw new TimeoutException (
411- $ "Task _getInstalledPackages for manager { Name } did not finish after "
412- + $ "{ PackageListingTaskTimeout } seconds, aborting. You may disable "
413- + $ "timeouts from UniGetUI Advanced Settings"
414- ) ;
415- }
416-
417- task . Wait ( ) ;
418- }
419-
420- var packages = task . GetAwaiter ( ) . GetResult ( ) ;
438+ var packages = RunListingTaskWithTimeout (
439+ GetInstalledPackages_UnSafe ,
440+ "_getInstalledPackages"
441+ ) ;
421442 Logger . Info ( $ "Found { packages . Count } installed packages from { Name } ") ;
422443 return packages ;
423444 }
424445 catch ( Exception e )
425446 {
426- if ( ! SecondAttempt )
447+ while ( e is AggregateException )
448+ e = e . InnerException ?? new InvalidOperationException ( "How did we get here?" ) ;
449+
450+ if ( ! SecondAttempt && ( RetryListingTasksOnTimeout || e is not TimeoutException ) )
427451 {
428- while ( e is AggregateException )
429- e =
430- e . InnerException
431- ?? new InvalidOperationException ( "How did we get here?" ) ;
432452 Logger . Warn (
433453 $ "Manager { DisplayName } failed to list installed packages with exception { e . GetType ( ) . Name } : { e . Message } "
434454 ) ;
0 commit comments