@@ -23,9 +23,9 @@ func getOldPIDFilePath(containerBaseName string) string {
2323 return filepath .Clean (filepath .Join (tmpDir , fmt .Sprintf ("toolhive-%s.pid" , containerBaseName )))
2424}
2525
26- // GetPIDFilePath returns the path to the PID file for a container
26+ // getPIDFilePath returns the path to the PID file for a container
2727// It first tries the new XDG location, then falls back to the old temp directory location
28- func GetPIDFilePath (containerBaseName string ) (string , error ) {
28+ func getPIDFilePath (containerBaseName string ) (string , error ) {
2929 // Return empty path in Kubernetes runtime since PID files are not used
3030 if runtime .IsKubernetesRuntime () {
3131 return "" , fmt .Errorf ("PID file operations are not supported in Kubernetes runtime" )
@@ -39,27 +39,27 @@ func GetPIDFilePath(containerBaseName string) (string, error) {
3939 return pidPath , nil
4040}
4141
42- // GetPIDFilePathWithFallback returns the path to an existing PID file for a container
42+ // getPIDFilePathWithFallback returns the path to an existing PID file for a container
4343// It checks both the new XDG location and the old temp directory location
4444// Note: containerBaseName is pre-sanitized by the caller
45- func GetPIDFilePathWithFallback (containerBaseName string ) (string , error ) {
45+ func getPIDFilePathWithFallback (containerBaseName string ) (string , error ) {
4646 // Return empty path in Kubernetes runtime since PID files are not used
4747 if runtime .IsKubernetesRuntime () {
4848 return "" , fmt .Errorf ("PID file operations are not supported in Kubernetes runtime" )
4949 }
5050
5151 // First try the new XDG-based path
52- newPath , err := GetPIDFilePath (containerBaseName )
52+ newPath , err := getPIDFilePath (containerBaseName )
5353 if err != nil {
5454 return "" , err
5555 }
5656
57- // Check if file exists at new location
57+ // Check if new file exists - prefer it if it does
5858 if _ , err := os .Stat (newPath ); err == nil {
5959 return newPath , nil
6060 }
6161
62- // Fall back to old location
62+ // Fall back to old location if new doesn't exist
6363 // Clean the path to satisfy security scanners (containerBaseName is already sanitized)
6464 oldPath := filepath .Clean (getOldPIDFilePath (containerBaseName ))
6565 if _ , err := os .Stat (oldPath ); err == nil {
@@ -81,11 +81,16 @@ func WritePIDFile(containerBaseName string, pid int) error {
8181 pidContent := []byte (fmt .Sprintf ("%d" , pid ))
8282
8383 // Write to the new XDG location first
84- newPath , err := GetPIDFilePath (containerBaseName )
84+ newPath , err := getPIDFilePath (containerBaseName )
8585 if err != nil {
8686 return fmt .Errorf ("failed to get PID file path: %v" , err )
8787 }
8888
89+ // Ensure the directory exists before writing
90+ if err := os .MkdirAll (filepath .Dir (newPath ), 0750 ); err != nil {
91+ return fmt .Errorf ("failed to create PID directory: %v" , err )
92+ }
93+
8994 if err := os .WriteFile (newPath , pidContent , 0600 ); err != nil {
9095 return fmt .Errorf ("failed to write PID file: %v" , err )
9196 }
@@ -114,7 +119,7 @@ func ReadPIDFile(containerBaseName string) (int, error) {
114119 }
115120
116121 // Get the PID file path with fallback
117- pidFilePath , err := GetPIDFilePathWithFallback (containerBaseName )
122+ pidFilePath , err := getPIDFilePathWithFallback (containerBaseName )
118123 if err != nil {
119124 return 0 , fmt .Errorf ("failed to get PID file path: %v" , err )
120125 }
@@ -159,7 +164,7 @@ func RemovePIDFile(containerBaseName string) error {
159164 var lastErr error
160165
161166 // Try to remove from the new location
162- newPath , err := GetPIDFilePath (containerBaseName )
167+ newPath , err := getPIDFilePath (containerBaseName )
163168 if err != nil {
164169 return fmt .Errorf ("failed to get PID file path: %v" , err )
165170 }
0 commit comments