-
Notifications
You must be signed in to change notification settings - Fork 212
fix(python apps): fork child + exec appcmd in child to avoid issues #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
a64679c
fix(python apps): fork a child and exec appcmd in the child to avoid …
sicoyle 480a884
fix: final clean testing with pipes for app prefix
sicoyle 6c802c1
Merge branch 'master' into fix-python-hangs
sicoyle 2492ef4
style: appease linter
sicoyle 9526f90
Merge branch 'fix-python-hangs' of ssh://github.com/sicoyle/dapr-cli …
sicoyle 27e160a
fix: make windows tests happy
sicoyle c51c42f
style: appease linter
sicoyle 647c648
fix: check for windows
sicoyle 9af9922
style: appease linter again
sicoyle def81e4
Merge branch 'master' into fix-python-hangs
JoshVanL 8e8516b
fix(build): update test
sicoyle f81bea0
Merge branch 'fix-python-hangs' of ssh://github.com/sicoyle/dapr-cli …
sicoyle 5b2eb25
Merge branch 'master' into fix-python-hangs
sicoyle 5e599f9
fix: updates for build to pass
sicoyle efe4d8a
Merge branch 'fix-python-hangs' of ssh://github.com/sicoyle/dapr-cli …
sicoyle e39d3a0
fix: ensure successful status on exit
sicoyle 3335080
fix(build): use more build tags to prevent windows os build failures
sicoyle 3bc794c
fix: use os specific cmds
sicoyle d4f9047
fix: updates for build
sicoyle a9b896e
fix: updates for build again
sicoyle f8460a7
fix: fixes for e2e tests
sicoyle 877699a
fix: final fixes for build i think
sicoyle 167668f
fix(tests): last update for tests
sicoyle a3abbd8
fix: add check to prevent unrelated panic
sicoyle b2cae33
fix: updates for e2e tests to be happy
sicoyle 08ae06a
fix: acct for diff responses in assert
sicoyle c9d9de9
fix: update to require to prevent panic
sicoyle 63922fa
fix: add early err check for same behavior
sicoyle 25702c8
fix: run app like normal on windows
sicoyle 5eb36f6
fix: update test accordingly
sicoyle ac00a1f
fix: handle windows app exec and test err paths
sicoyle ade38f1
fix(tests): update for more windows speciftic cmds
sicoyle 99160ae
style: updates per feedback
sicoyle fd34ce5
fix(tests): allow time for ports to be released in cleanup
sicoyle 4d80fc6
fix: kill processes launched via shell exec
sicoyle 5c48a85
fix: handle app already exiting naturally
sicoyle 0f939d4
fix: handle when process already exited
sicoyle cd0dc9a
fix: add delay for tests
sicoyle 9523307
fix: handle diff order for test output
sicoyle cfa739f
style: random comment to bump ci
sicoyle e6e90d2
Merge branch 'master' into fix-python-hangs
JoshVanL e6258f9
fix: updates based on ai code review
sicoyle 56f50b5
Merge branch 'fix-python-hangs' of ssh://github.com/sicoyle/dapr-cli …
sicoyle a7898c5
fix: update for test
sicoyle af6b33e
fix: handle cases without app cmd in test
sicoyle 9f4e89a
fix: pin docker version to fix unrelated testissues
sicoyle 2c217a7
fix: allow time for port releases
sicoyle 5b36220
fix: address feedback
sicoyle 2f65eef
fix: see if this helps with the e2e faliures
sicoyle 73bf225
fix: up the times
sicoyle e96aebc
fix: up timeouts
sicoyle c91b919
fix: handle scheudler port conflict issue flake
sicoyle e1a10e8
fix: update to fix flakey scheduler test
sicoyle 9ee93d4
style: updates per feedback
sicoyle 1c4af6c
fix: update for test
sicoyle 88883d9
fix: address final feedback
sicoyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //go:build !windows | ||
|
|
||
| /* | ||
| Copyright 2026 The Dapr Authors | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "syscall" | ||
|
|
||
| "github.com/dapr/cli/pkg/print" | ||
| runExec "github.com/dapr/cli/pkg/runexec" | ||
| ) | ||
|
|
||
| // setDaprProcessGroupForRun sets the process group on the daprd command so the | ||
| // sidecar can be managed independently (e.g. when the app is started via exec). | ||
| func setDaprProcessGroupForRun(cmd *exec.Cmd) { | ||
| if cmd == nil { | ||
| return | ||
| } | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
| Setpgid: true, | ||
| } | ||
| } | ||
|
|
||
| // startAppProcessInBackground starts the app process using ForkExec. | ||
| // This prevents the child from seeing a fork, avoiding Python async/threading issues, | ||
| // and sets output.AppCMD.Process. | ||
| // It then runs a goroutine that waits and signals sigCh. | ||
| func startAppProcessInBackground(output *runExec.RunOutput, binary string, args []string, env []string, sigCh chan os.Signal) error { | ||
| if output.AppCMD == nil || output.AppCMD.Process != nil { | ||
| return fmt.Errorf("app command is nil") | ||
| } | ||
|
|
||
| procAttr := &syscall.ProcAttr{ | ||
| Env: env, | ||
| // stdin, stdout, and stderr inherit directly from the parent | ||
| // This prevents Python from detecting pipes because if the app is Python then it will detect the pipes and think | ||
| // it's a fork and will cause random hangs due to async python in durabletask-python. | ||
| Files: []uintptr{0, 1, 2}, | ||
| Sys: &syscall.SysProcAttr{ | ||
| Setpgid: true, | ||
| }, | ||
| } | ||
|
|
||
| // Use ForkExec to fork a child, then exec python in the child. | ||
| // NOTE: This is needed bc forking a python app with async python running (i.e., everything in durabletask-python) will cause random hangs, no matter the python version. | ||
| // Doing this this way makes python not see the fork, starts via exec, so it doesn't cause random hangs due to when forking async python apps where locks and such get corrupted in forking. | ||
| argv := append([]string{binary}, args[1:]...) | ||
| pid, err := syscall.ForkExec(binary, argv, procAttr) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fork/exec app: %w", err) | ||
| } | ||
| output.AppCMD.Process = &os.Process{Pid: pid} | ||
sicoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| go func() { | ||
| var waitStatus syscall.WaitStatus | ||
| _, err := syscall.Wait4(pid, &waitStatus, 0, nil) | ||
| if err != nil { | ||
| output.AppErr = err | ||
| print.FailureStatusEvent(os.Stderr, "The App process exited with error: %s", err.Error()) | ||
| } else if waitStatus.Signaled() { | ||
| output.AppErr = fmt.Errorf("app terminated by signal: %s", waitStatus.Signal()) | ||
| print.FailureStatusEvent(os.Stderr, "The App process was terminated by signal: %s", waitStatus.Signal()) | ||
| } else if waitStatus.Exited() && waitStatus.ExitStatus() != 0 { | ||
| output.AppErr = fmt.Errorf("app exited with status %d", waitStatus.ExitStatus()) | ||
| print.FailureStatusEvent(os.Stderr, "The App process exited with error code: %d", waitStatus.ExitStatus()) | ||
| } else { | ||
| print.SuccessStatusEvent(os.Stdout, "Exited App successfully") | ||
| } | ||
| sigCh <- os.Interrupt | ||
| }() | ||
sicoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //go:build windows | ||
|
|
||
| /* | ||
| Copyright 2026 The Dapr Authors | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "github.com/dapr/cli/pkg/print" | ||
| runExec "github.com/dapr/cli/pkg/runexec" | ||
| ) | ||
|
|
||
| // setDaprProcessGroupForRun is a no-op on Windows (SysProcAttr.Setpgid does not exist). | ||
| func setDaprProcessGroupForRun(cmd *exec.Cmd) { | ||
| // no-op on Windows | ||
| _ = cmd | ||
| } | ||
|
|
||
| // startAppProcessInBackground starts the app process using exec.Command, | ||
| // sets output.AppCMD to the new command, and runs a goroutine that waits and signals sigCh. | ||
| func startAppProcessInBackground(output *runExec.RunOutput, binary string, args []string, env []string, sigCh chan os.Signal) error { | ||
| cmdArgs := args[1:] | ||
| if output.AppCMD == nil { | ||
| output.AppCMD = exec.Command(binary, cmdArgs...) | ||
| } else { | ||
| output.AppCMD.Path = binary | ||
sicoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| output.AppCMD.Args = append([]string{binary}, cmdArgs...) | ||
| } | ||
| output.AppCMD.Env = env | ||
| output.AppCMD.Stdin = os.Stdin | ||
| output.AppCMD.Stdout = os.Stdout | ||
| output.AppCMD.Stderr = os.Stderr | ||
|
|
||
| if err := output.AppCMD.Start(); err != nil { | ||
| return fmt.Errorf("failed to start app: %w", err) | ||
| } | ||
|
|
||
| go func() { | ||
| waitErr := output.AppCMD.Wait() | ||
| if waitErr != nil { | ||
| output.AppErr = waitErr | ||
| print.FailureStatusEvent(os.Stderr, "The App process exited with error: %s", waitErr.Error()) | ||
| } else if output.AppCMD.ProcessState != nil && !output.AppCMD.ProcessState.Success() { | ||
| output.AppErr = fmt.Errorf("app exited with status %d", output.AppCMD.ProcessState.ExitCode()) | ||
| print.FailureStatusEvent(os.Stderr, "The App process exited with error code: %d", output.AppCMD.ProcessState.ExitCode()) | ||
| } else { | ||
| print.SuccessStatusEvent(os.Stdout, "Exited App successfully") | ||
| } | ||
| sigCh <- os.Interrupt | ||
| }() | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.