@cryptotaxi247 / kubo / commits / afd497e19

Dirty hack to fix race conditions in the daemon

Konstantin Koroviev committed Mar 9, 2015 at 15:43 UTC afd497e194e8374b848a3ab2f91f8b820010035f
3 files changed +19 -3
cmd/ipfs/daemon.go
+4
@@ -267,6 +267,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
267 gateway.ServeOption(),
268 corehttp.VersionOption(),
269 }
270 +
271 + // our global interrupt handler can now try to stop the daemon
272 + close(req.Context().ContextIsReadyToBeClosed)
273 +
274 if rootRedirect != nil {
275 opts = append(opts, rootRedirect)
276 }
cmd/ipfs/main.go
+11
@@ -132,6 +132,14 @@ func main() {
132 os.Exit(1)
133 }
134
135 + // our global interrupt handler may try to stop the daemon
136 + // before the daemon is ready to be stopped; this dirty
137 + // workaround is for the daemon only; other commands are always
138 + // ready to be stopped
139 + if invoc.cmd != daemonCmd {
140 + close(invoc.req.Context().ContextIsReadyToBeClosed)
141 + }
142 +
143 // ok, finally, run the command invocation.
144 output, err := invoc.Run(ctx)
145 if err != nil {
@@ -473,6 +481,9 @@ func (i *cmdInvocation) setupInterruptHandler() {
481 sig := allInterruptSignals()
482
483 go func() {
484 + // wait till the context is ready to be closed
485 + <-ctx.ContextIsReadyToBeClosed
486 +
487 // first time, try to shut down.
488
489 // loop because we may be
commands/request.go
+4 -3
@@ -28,8 +28,9 @@ type Context struct {
28 config *config.Config
29 LoadConfig func(path string) (*config.Config, error)
30
31 - node *core.IpfsNode
32 - ConstructNode func() (*core.IpfsNode, error)
31 + node *core.IpfsNode
32 + ConstructNode func() (*core.IpfsNode, error)
33 + ContextIsReadyToBeClosed chan bool
34 }
35
36 // GetConfig returns the config of the current Command exection
@@ -287,7 +288,7 @@ func NewRequest(path []string, opts OptMap, args []string, file files.File, cmd
288 optDefs = make(map[string]Option)
289 }
290
290 - ctx := Context{Context: context.TODO()}
291 + ctx := Context{Context: context.TODO(), ContextIsReadyToBeClosed: make(chan bool)}
292 values := make(map[string]interface{})
293 req := &request{path, opts, args, file, cmd, ctx, optDefs, values, os.Stdin}
294 err := req.ConvertOptions()