55using System . Linq ;
66using System . Reflection ;
77using System . Runtime . CompilerServices ;
8- using System . Security . Cryptography ;
98using System . Security . Claims ;
9+ using System . Security . Cryptography ;
1010using System . Text ;
1111using System . Threading ;
1212using System . Threading . Tasks ;
@@ -406,12 +406,27 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
406406 try
407407 {
408408 Trace . Info ( nameof ( RunAsync ) ) ;
409-
409+
410+ // Validate directory permissions.
411+ string workDirectory = HostContext . GetDirectory ( WellKnownDirectory . Work ) ;
412+ Trace . Info ( $ "Validating directory permissions for: '{ workDirectory } '") ;
413+ try
414+ {
415+ Directory . CreateDirectory ( workDirectory ) ;
416+ IOUtil . ValidateExecutePermission ( workDirectory ) ;
417+ }
418+ catch ( Exception ex )
419+ {
420+ Trace . Error ( ex ) ;
421+ _term . WriteError ( $ "Fail to create and validate runner's work directory '{ workDirectory } '.") ;
422+ return Constants . Runner . ReturnCode . TerminatedError ;
423+ }
424+
410425 // First try using migrated settings if available
411426 var configManager = HostContext . GetService < IConfigurationManager > ( ) ;
412427 RunnerSettings migratedSettings = null ;
413-
414- try
428+
429+ try
415430 {
416431 migratedSettings = configManager . LoadMigratedSettings ( ) ;
417432 Trace . Info ( "Loaded migrated settings from .runner_migrated file" ) ;
@@ -422,15 +437,15 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
422437 // If migrated settings file doesn't exist or can't be loaded, we'll use the provided settings
423438 Trace . Info ( $ "Failed to load migrated settings: { ex . Message } ") ;
424439 }
425-
440+
426441 bool usedMigratedSettings = false ;
427-
442+
428443 if ( migratedSettings != null )
429444 {
430445 // Try to create session with migrated settings first
431446 Trace . Info ( "Attempting to create session using migrated settings" ) ;
432447 _listener = GetMessageListener ( migratedSettings , isMigratedSettings : true ) ;
433-
448+
434449 try
435450 {
436451 CreateSessionResult createSessionResult = await _listener . CreateSessionAsync ( HostContext . RunnerShutdownToken ) ;
@@ -450,7 +465,7 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
450465 Trace . Error ( $ "Exception when creating session with migrated settings: { ex } ") ;
451466 }
452467 }
453-
468+
454469 // If migrated settings weren't used or session creation failed, use original settings
455470 if ( ! usedMigratedSettings )
456471 {
@@ -503,7 +518,7 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
503518 restartSession = true ;
504519 break ;
505520 }
506-
521+
507522 TaskAgentMessage message = null ;
508523 bool skipMessageDeletion = false ;
509524 try
@@ -859,7 +874,7 @@ private async Task<int> ExecuteRunnerAsync(RunnerSettings settings, bool runOnce
859874 {
860875 restart = false ;
861876 returnCode = await RunAsync ( settings , runOnce ) ;
862-
877+
863878 if ( returnCode == Constants . Runner . ReturnCode . RunnerConfigurationRefreshed )
864879 {
865880 Trace . Info ( "Runner configuration was refreshed, restarting session..." ) ;
0 commit comments