mesh-sdk: a bus function called before Serve waits for Serve instead of refusing (issue 1966)
A module that starts a goroutine before stdio.Serve and calls the bus there was refused and retried on its own tick, a minute later. Refused now only when the process never serves. Claude-Session: https://claude.ai/code/session_01NcbgfFSnn24M3GcqttoU7i
This commit is contained in:
+28
-7
@@ -441,13 +441,36 @@ func ServeOn(name string, tools []Tool, in io.Reader, out io.Writer) error {
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func live() (*channel, error) {
|
||||
// serveWait is how long a bus function called before Serve waits for Serve to have the runtime's channel. A bundle
|
||||
// that starts a goroutine before stdio.Serve and calls the bus there finds the channel a moment later; only a
|
||||
// process that never serves is refused, after this long.
|
||||
var serveWait = 30 * time.Second
|
||||
|
||||
// awaitChannel is the runtime's channel, waiting up to serveWait for Serve to set it.
|
||||
func awaitChannel() *channel {
|
||||
currentMu.Lock()
|
||||
c, ready := current, served
|
||||
currentMu.Unlock()
|
||||
if c != nil {
|
||||
return c
|
||||
}
|
||||
t := time.NewTimer(serveWait)
|
||||
defer t.Stop()
|
||||
select {
|
||||
case <-ready:
|
||||
case <-t.C:
|
||||
return nil
|
||||
}
|
||||
currentMu.Lock()
|
||||
defer currentMu.Unlock()
|
||||
if current == nil {
|
||||
return nil, errors.New("before Serve: a bundle reaches the bus through the runtime that launched it")
|
||||
return current
|
||||
}
|
||||
|
||||
func live() (*channel, error) {
|
||||
if c := awaitChannel(); c != nil {
|
||||
return c, nil
|
||||
}
|
||||
return current, nil
|
||||
return nil, errors.New("before Serve: a bundle reaches the bus through the runtime that launched it")
|
||||
}
|
||||
|
||||
// Subscribe asks the runtime for this module's events whose type matches the pattern (`*` one
|
||||
@@ -571,9 +594,7 @@ type EmitOptions struct {
|
||||
// accepted it (ADR 0193). The envelope and headers are the TypeScript SDK's exactly: the source and
|
||||
// node come from MESH_MODULE and MESH_NODE, which the runtime sets.
|
||||
func Emit(eventType string, body any, opts ...EmitOptions) error {
|
||||
currentMu.Lock()
|
||||
c := current
|
||||
currentMu.Unlock()
|
||||
c := awaitChannel()
|
||||
if c == nil {
|
||||
return errors.New("emit before Serve: a bundle emits through the runtime that launched it")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user