Comments (6)
Yes, it is intentional. If nothing ever runs there's no way to properly flag the tomb's death. You should also be able to use the Dead and Dying channels instead of Wait for holding out, for instance, and there's no means for us to guess that these channels have to be closed if the tomb is never called.
from tomb.
As a side note, the example provided feels unnecessarily hackish. You can simply do the obvious:
func rmain() error {
if somethingBad {
return err
}
...
obj.tomb.Go(obj.action)
return obj.tomb.Wait()
}
I also more often organize it as:
func StartFoo() (*Foo, error) {
...
foo.tomb.Go(foo.loop)
return foo, nil
}
func (foo *Foo) Stop() error {
foo.tomb.Kill(nil)
return foo.tomb.Wait()
}
from tomb.
Ok, thanks!
The reason I wanted to do it is because I have code between the Go call and the Wait call, like this:
obj.tomb.Go(obj.action)
if err := ...; err != nil {
// return err // whoops, no waiting, can't do this
obj.tomb.Kill(err) // at least we'll wait, but it's also going to execute functions it shouldn't
}
if err := ...; err != nil {
// ...
}
return obj.tomb.Wait()
from tomb.
There are two easy ways to solve the problem:
- Do not call Go while you're not ready to spawn a new goroutine
- Move the logic that can fail to within the goroutine
Both sound like good advices despite the specific issue being discussed.
from tomb.
Do not call Go while you're not ready to spawn a new goroutine
It's not that I'm not ready, in fact it's better if the goroutine already started before I run my other functions.
Move the logic that can fail to within the goroutine
Unfortunately, they need to run concurrently to the goroutine. Or are you saying I should run an unmonitored goroutine within the Go
-routine?
from tomb.
You can run as many things concurrently as desired by just firing more monitored goroutines from any goroutine already being monitored. For example:
func StartFoo() (*Foo, error) {
...
foo.tomb.Go(foo.loop)
return foo, nil
}
func (foo *Foo) Stop() error {
foo.tomb.Kill(nil)
return foo.tomb.Wait()
}
func (foo *Foo) loop() error {
foo.tomb.Go(foo.task1)
foo.tomb.Go(foo.task2)
...
}
from tomb.
Related Issues (17)
- Please keep source gofmt'ed HOT 1
- Usage Question / panic, tomb.go called after all goroutines terminated
- please make a release HOT 4
- Wait() never unblocks when no goroutine is running HOT 1
- Panics can happen if the first callback finishes very quickly HOT 2
- Wait() hangs forever if no goroutine was run with Go() HOT 7
- Would be nice to have `Running()` and `Terminating()`
- Bug: Tomb `Wait` blocks forever when no Goroutine is ever spawned. HOT 2
- Inactivity ... I would like to volunteer/help-out
- Seemingly impossible panic: tomb.Go called after all goroutines terminated HOT 2
- go test error
- License BSD3
- tomb kills self after the last goroutine completes HOT 1
- gopkg.in/[email protected]: invalid pseudo-version: does not match version-control timestamp (expected 20161208151619) HOT 1
- `Wait()` deadlocks after `Kill()` if `Go()` has never been called HOT 1
- Use sync.WaitGroup instead?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tomb.