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.
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.
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.
Do you want to make it impossible to call
JobSpawnwithoutonExitcallback?
No, I don't. Take a closer look at the above code.
There was a problem hiding this comment.
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.
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 JobSpawnnil callback for JobSpawn
If a program started with
shell.JobSpawnterminates and no callback is set, then micro crashes. For example, if I call the Lua functionand close the
xclockwindow, then I get ((on Ubuntu, with master) :This PR fixes this bug.