-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
avoid creating nil
callback for JobSpawn
#3554
Conversation
cmd/micro/micro.go
Outdated
f.Function(f.Output, f.Args) | ||
if f.Function != nil { | ||
f.Function(f.Output, f.Args) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we rather prevent creating a dummy "job" in the first place? I.e.:
diff --git a/internal/shell/job.go b/internal/shell/job.go
index 6e1f4b18..766b9516 100644
--- a/internal/shell/job.go
+++ b/internal/shell/job.go
@@ -78,8 +78,10 @@ func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit func(
go func() {
// Run the process in the background and create the onExit callback
proc.Run()
- jobFunc := JobFunction{onExit, outbuf.String(), userargs}
- Jobs <- jobFunc
+ if onExit != nil {
+ jobFunc := JobFunction{onExit, outbuf.String(), userargs}
+ Jobs <- jobFunc
+ }
}()
return &Job{proc, stdin}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to make it impossible to call JobSpawn
without onExit
callback? I think there are cases where you have no callback. For example, I'm currently trying to write some LaTeX support for micro. One functionality is to start a PDF viewer to see the generated PDF file. I start it with JobSpawn
and no callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to make it impossible to call
JobSpawn
withoutonExit
callback?
No, I don't. Take a closer look at the above code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not being an expert in your code. So Run
would start the program and Jobs
is just there to handle the result. In that case your suggestion makes sense. Shall I update the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the Jobs
channel is there to let DoEvent()
in the main goroutine execute the onExit
callback. So if there is no onExit
callback, no point in sending anything to the Jobs
channel in the first place.
Shall I update the PR?
Yes, if it works for you.
9eca5cd
to
164d991
Compare
PR updated according to @dmaluka's suggestion |
nil
before executing callback for JobSpawn
nil
callback for JobSpawn
If a program started with
shell.JobSpawn
terminates and no callback is set, then micro crashes. For example, if I call the Lua functionand close the
xclock
window, then I get ((on Ubuntu, with master) :This PR fixes this bug.