@@ -441,7 +441,7 @@ fn rollback_registration_with(
441441 definition_result,
442442 disable_result,
443443 ) ;
444- if previous. is_some_and ( |snapshot| !snapshot. enabled ) {
444+ if rollback_result . is_err ( ) || previous. is_some_and ( |snapshot| !snapshot. enabled ) {
445445 let final_disable_result = api. disable_for_rollback ( ) ;
446446 return combine_task_operations (
447447 "restore disabled daemon task registration" ,
@@ -1328,7 +1328,7 @@ mod tests {
13281328 task : Option < TaskSnapshot > ,
13291329 xml : Option < String > ,
13301330 operations : Vec < Operation > ,
1331- fail_registration_after_mutation : bool ,
1331+ registration_failures_remaining : usize ,
13321332 fail_next_enablement : bool ,
13331333 fail_next_run : bool ,
13341334 fail_next_stop : bool ,
@@ -1351,7 +1351,7 @@ mod tests {
13511351 task : Some ( TaskSnapshot { running, enabled } ) ,
13521352 xml : Some ( xml. to_string ( ) ) ,
13531353 operations : Vec :: new ( ) ,
1354- fail_registration_after_mutation : false ,
1354+ registration_failures_remaining : 0 ,
13551355 fail_next_enablement : false ,
13561356 fail_next_run : false ,
13571357 fail_next_stop : false ,
@@ -1391,7 +1391,8 @@ mod tests {
13911391 enabled : true ,
13921392 } ) ;
13931393 self . xml = Some ( xml. to_string ( ) ) ;
1394- if std:: mem:: take ( & mut self . fail_registration_after_mutation ) {
1394+ if self . registration_failures_remaining > 0 {
1395+ self . registration_failures_remaining -= 1 ;
13951396 return Err ( TraceDecayError :: Config {
13961397 message : "fake scheduler registration failed after mutation" . to_string ( ) ,
13971398 } ) ;
@@ -1857,7 +1858,7 @@ mod tests {
18571858 fn registration_api_failure_after_mutation_restores_disabled_state ( ) {
18581859 let mut api =
18591860 FakeTaskScheduler :: with_task ( DaemonServiceState :: StoppedDisabled , "<Task>old</Task>" ) ;
1860- api. fail_registration_after_mutation = true ;
1861+ api. registration_failures_remaining = 1 ;
18611862
18621863 let error = register_task_xml_with ( & mut api, "<Task>new</Task>" )
18631864 . expect_err ( "registration must fail" ) ;
@@ -1875,6 +1876,20 @@ mod tests {
18751876 ) ;
18761877 }
18771878
1879+ #[ test]
1880+ fn failed_enabled_task_definition_rollback_disables_residual_task ( ) {
1881+ let mut api =
1882+ FakeTaskScheduler :: with_task ( DaemonServiceState :: StoppedEnabled , "<Task>old</Task>" ) ;
1883+ api. registration_failures_remaining = 2 ;
1884+
1885+ let error = register_task_xml_with ( & mut api, "<Task>new</Task>" )
1886+ . expect_err ( "registration and rollback must fail" ) ;
1887+
1888+ assert ! ( error. to_string( ) . contains( "state restoration also failed" ) ) ;
1889+ assert_eq ! ( api. state( ) , DaemonServiceState :: StoppedDisabled ) ;
1890+ assert_eq ! ( api. operations. last( ) , Some ( & Operation :: Enable ( false ) ) ) ;
1891+ }
1892+
18781893 #[ test]
18791894 fn failed_new_registration_cleanup_leaves_residual_task_disabled ( ) {
18801895 let mut api =
0 commit comments