Browse Source

kill process if there was an error during reading to not leave a zombie

Laytan Laats 10 months ago
parent
commit
77b033cf96
1 changed files with 43 additions and 40 deletions
  1. 43 40
      core/os/os2/process.odin

+ 43 - 40
core/os/os2/process.odin

@@ -395,54 +395,57 @@ process_exec :: proc(
 		process = process_start(desc) or_return
 		process = process_start(desc) or_return
 	}
 	}
 
 
-	stdout_builder := strings.builder_make(allocator) or_return
-	stderr_builder := strings.builder_make(allocator) or_return
-
-	has_stdout, has_stderr: bool
-	read_data: for !has_stdout || !has_stderr {
-		buf: [1024]u8 = ---
-		n: int
-		has_data: bool
-
-		if !has_stdout {
-			has_data, err = pipe_has_data(stdout_r)
-			if has_data {
-				n, err = read(stdout_r, buf[:])
-				if strings.write_bytes(&stdout_builder, buf[:n]) != n {
-					err = .Out_Of_Memory
+	{
+		defer if err != nil { _ = process_kill(process) }
+
+		stdout_builder := strings.builder_make(allocator) or_return
+		stderr_builder := strings.builder_make(allocator) or_return
+
+		has_stdout, has_stderr: bool
+		read_data: for !has_stdout || !has_stderr {
+			buf: [1024]u8 = ---
+			n: int
+			has_data: bool
+
+			if !has_stdout {
+				has_data, err = pipe_has_data(stdout_r)
+				if has_data {
+					n, err = read(stdout_r, buf[:])
+					if strings.write_bytes(&stdout_builder, buf[:n]) != n {
+						err = .Out_Of_Memory
+					}
+				}
+				switch err {
+				case nil: // nothing
+				case .EOF, .Broken_Pipe:
+					stdout     = stdout_builder.buf[:]
+					has_stdout = true
+				case:
+					return
 				}
 				}
 			}
 			}
-			switch err {
-			case nil: // nothing
-			case .EOF, .Broken_Pipe:
-				stdout     = stdout_builder.buf[:]
-				has_stdout = true
-			case:
-				return
-			}
-		}
 
 
-		if !has_stderr {
-			has_data, err = pipe_has_data(stderr_r)
-			if has_data {
-				n, err = read(stderr_r, buf[:])
-				if strings.write_bytes(&stderr_builder, buf[:n]) != n {
-					err = .Out_Of_Memory
+			if !has_stderr {
+				has_data, err = pipe_has_data(stderr_r)
+				if has_data {
+					n, err = read(stderr_r, buf[:])
+					if strings.write_bytes(&stderr_builder, buf[:n]) != n {
+						err = .Out_Of_Memory
+					}
+				}
+				switch err {
+				case nil: // nothing
+				case .EOF, .Broken_Pipe:
+					stderr     = stderr_builder.buf[:]
+					has_stderr = true
+				case:
+					return
 				}
 				}
-			}
-			switch err {
-			case nil: // nothing
-			case .EOF, .Broken_Pipe:
-				stderr     = stderr_builder.buf[:]
-				has_stderr = true
-			case:
-				return
 			}
 			}
 		}
 		}
 	}
 	}
 
 
-	err = nil
-	state = process_wait(process) or_return
+	state, err = process_wait(process)
 	return
 	return
 }
 }