From 3179871dfe597f763b9fb45590b07a0679b0a628 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 16 Nov 2026 21:06:27 -0800 Subject: [PATCH] Recover Windows cleanup when a scheduled task is already absent Accept the CLR mapping of a missing Task Scheduler task and retry older worker cleanup through its pinned assembly. Early desktop startup must not leave a VM permanently requiring recovery. Assisted-by: gpt-6.1-sol --- guest/computer-windows.cs | 2 +- tools/computer-test.py | 27 +++++++++++++++++++++++++++ tools/vm-computer.py | 21 +++++++++++++++------ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/guest/computer-windows.cs b/guest/computer-windows.cs index 316df9ad2028cabae8ee6f729b8638f5ab34ed17..88349cd3c1fd7e1bff75c877c2dc76ce73b81785 100644 --- a/guest/computer-windows.cs +++ b/guest/computer-windows.cs @@ -196,7 +196,7 @@ public static class ComputerWorker { service.Connect(null,null,null,null); dynamic folder=service.GetFolder("\\"); try { folder.DeleteTask("SnowglobeComputer-"+System.IO.Path.GetFileName(directory),0); } - catch(COMException error) { if(error.ErrorCode!=unchecked((int)0x80070002))throw; } + catch(Exception error) { if(error.HResult!=unchecked((int)0x80070002))throw; } if(!keepSpawn && System.IO.Directory.Exists(directory))System.IO.Directory.Delete(directory,true); } static object Target(System.Windows.Automation.AutomationElement node) { diff --git a/tools/computer-test.py b/tools/computer-test.py index 2098557c19a50642a535c76a82dcf57a394d40a5..9afb69879f8a76de1fcd413addffa77d87e72c59 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -363,6 +363,33 @@ class GuestCancellation(unittest.TestCase): self.assertEqual(result['stdout'], 'ready') self.assertEqual([call.args[2][0] for call in execute.call_args_list], ['--prepare', '--launch', '--cleanup']) + def test_failed_native_cleanup_recovers_through_the_pinned_worker(self): + operation = {'platform': 'windows', 'id': 'a' * 32, + 'worker': 'C:\\ProgramData\\SnowglobeComputer\\worker-previous.exe'} + computer.record_operation('fixture', operation) + with patch.object(computer, 'guest_exec', side_effect=ValueError('Worker exited 1')), patch.object(computer, 'powershell', return_value=b'') as fallback: + computer.finish_operation('fixture', operation) + script = fallback.call_args.args[1] + self.assertIn("LoadFrom('" + operation['worker'] + "')", script) + self.assertIn('[ComputerProcess]::Stop', script) + self.assertFalse((computer.vms.DISKS / 'fixture' / 'computer-operation.json').exists()) + + def test_failed_native_and_fallback_cleanup_preserves_recovery(self): + operation = {'platform': 'windows', 'id': 'a' * 32, 'worker': 'previous.exe'} + computer.record_operation('fixture', operation) + with patch.object(computer, 'guest_exec', side_effect=ValueError('Worker exited 1')), patch.object(computer, 'powershell', side_effect=ValueError('Disconnected')): + with self.assertRaises(BlockingIOError): + computer.finish_operation('fixture', operation) + self.assertEqual(json.loads((computer.vms.DISKS / 'fixture' / 'computer-operation.json').read_text()), operation) + + def test_spawn_cleanup_fallback_keeps_the_process_running(self): + operation = {'platform': 'windows', 'id': 'a' * 32, 'worker': 'previous.exe', 'keep_spawn': True} + computer.record_operation('fixture', operation) + with patch.object(computer, 'guest_exec', side_effect=ValueError('Worker exited 1')), patch.object(computer, 'powershell', return_value=b'') as fallback: + computer.finish_operation('fixture', operation) + self.assertNotIn('[ComputerProcess]::Stop', fallback.call_args.args[1]) + self.assertNotIn('$t.Stop(0)', fallback.call_args.args[1]) + def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self): units = [] def execute(name, executable, arguments, data=None, **kwargs): diff --git a/tools/vm-computer.py b/tools/vm-computer.py index 3f2d06a0c3c41bf9e264d50bd17fe7b54a8573e7..695f272bd1adefb682bbdf4d0cc41441cd401f23 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -372,20 +372,29 @@ def finish_operation(name, operation): json.dumps({'_stop': operation['id']}).encode(), timeout=18) elif operation.get('worker'): directory = 'C:\\ProgramData\\SnowglobeComputer\\' + operation['id'] - guest_exec(name, operation['worker'], ['--release' if operation.get('keep_spawn') else '--cleanup', directory], timeout=58) - else: + try: + guest_exec(name, operation['worker'], ['--release' if operation.get('keep_spawn') else '--cleanup', directory], timeout=58) + except ValueError: + pass + else: + (vms.DISKS / name / 'computer-operation.json').unlink() + return + if operation['platform'] == 'windows': run = operation['id'] directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run cleanup = "$ErrorActionPreference='Stop';$d='"+directory+"';" cleanup += "if(Test-Path -LiteralPath ($d+'\\request.json')){Remove-Item -LiteralPath ($d+'\\request.json') -Force};" cleanup += "$s=New-Object -ComObject Schedule.Service;$s.Connect();$f=$s.GetFolder('\\');" - cleanup += "$t=$f.GetTasks(1) | Where-Object {$_.Name -eq 'SnowglobeComputer-"+run+"'};if($t){" + cleanup += "$t=$f.GetTasks(1) | Where-Object {$_.Name -eq 'SnowglobeComputer-"+run+"'};" if not operation.get('keep_spawn'): + if operation.get('worker'): + cleanup += "[Reflection.Assembly]::LoadFrom('"+operation['worker']+"')|Out-Null;[ComputerProcess]::Stop($d+'\\request.json');" cleanup += "$workers=@(Get-WmiObject Win32_Process | Where-Object {$_.CommandLine -like ('*'+$d+'\\request.json*')} | ForEach-Object {$p=Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue;if($p){$null=$p.Handle;$p}});" - cleanup += "[Reflection.Assembly]::LoadFrom($t.Definition.Actions.Item(1).Path)|Out-Null;[ComputerProcess]::Stop($d+'\\request.json');" - cleanup += "$t.Stop(0);while($t.State -eq 4){Start-Sleep -Milliseconds 100};" + if not operation.get('worker'): + cleanup += "if($t){[Reflection.Assembly]::LoadFrom($t.Definition.Actions.Item(1).Path)|Out-Null;[ComputerProcess]::Stop($d+'\\request.json')};" + cleanup += "if($t){$t.Stop(0);while($t.State -eq 4){Start-Sleep -Milliseconds 100}};" cleanup += "foreach($p in $workers){if(!$p.WaitForExit(10000)){throw 'Process cleanup failed'};$p.Dispose()};" - cleanup += "$f.DeleteTask('SnowglobeComputer-"+run+"',0)};" + cleanup += "if($t){$f.DeleteTask('SnowglobeComputer-"+run+"',0)};" if not operation.get('keep_spawn'): cleanup += "if(Test-Path -LiteralPath $d){Remove-Item -LiteralPath $d -Recurse -Force}" powershell(name, cleanup, timeout=58) -- 2.54.1