From 9e8bf6dc0c0af7c03af5b30a9e4133d1f24c47c0 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 15 Nov 2026 01:45:37 +0100 Subject: [PATCH] Runner: a get session's object reader reads on a context that ends with the session, not with the call that opened it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds: design 47 G §490, §518, §620 (a get over one chunk is a session read chunk by chunk) --- node-tools/internal/runtime/busstate.go | 37 ++++++++++++++++---- node-tools/internal/runtime/sessions_test.go | 22 +++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/node-tools/internal/runtime/busstate.go b/node-tools/internal/runtime/busstate.go index 567c3de..7cbc110 100644 --- a/node-tools/internal/runtime/busstate.go +++ b/node-tools/internal/runtime/busstate.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "sort" + "time" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/buscalls" @@ -177,15 +178,18 @@ func (c *consumers) objectsGet(module string, r buscalls.ObjectsNameRequest) (an if st == nil { return nil, fmt.Errorf("the runner has no bus to read %s of %s from", r.Name, module) } - ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout) - defer cancel() + ctx, cancel, opened := openingContext() info, rd, found, err := st.ObjectsOpen(ctx, module, r) + opened() if err != nil || !found { + cancel() return nil, err } if info.Size > buscalls.ChunkBytes { - return c.openGet(module, info, rd, sessionDigest(info.Digest), false) + // the reader reads on ctx after this call returns: it ends with the session, not with this call + return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(info.Digest), false) } + defer cancel() defer rd.Close() data, err := io.ReadAll(rd) if err != nil { @@ -208,11 +212,32 @@ func (c *consumers) sentGet(module string, r buscalls.ObjectsSentGetRequest) (an if st == nil { return nil, fmt.Errorf("the runner has no bus to read %s from", r.Object) } - ctx, cancel := context.WithTimeout(context.Background(), bus.StateTimeout) - defer cancel() + ctx, cancel, opened := openingContext() info, rd, err := st.SentOpen(ctx, r) + opened() if err != nil { + cancel() return nil, err } - return c.openGet(module, info, rd, sessionDigest(r.Digest), true) + return c.openGet(module, info, cancelOnClose{rd, cancel}, sessionDigest(r.Digest), true) +} + +// openingContext is the context an object is opened on: it is bound at bus.StateTimeout while opening, and once opened (the +// returned function) is bound no more: a reader of an object that outlives the call reads on it until its session ends. +func openingContext() (context.Context, context.CancelFunc, func()) { + ctx, cancel := context.WithCancel(context.Background()) + t := time.AfterFunc(bus.StateTimeout, cancel) + return ctx, cancel, func() { t.Stop() } +} + +// cancelOnClose ends the context an object's reader reads on when the reader is closed: at the end of its get session. +type cancelOnClose struct { + io.ReadCloser + cancel context.CancelFunc +} + +func (c cancelOnClose) Close() error { + err := c.ReadCloser.Close() + c.cancel() + return err } diff --git a/node-tools/internal/runtime/sessions_test.go b/node-tools/internal/runtime/sessions_test.go index 4d67f94..97adf21 100644 --- a/node-tools/internal/runtime/sessions_test.go +++ b/node-tools/internal/runtime/sessions_test.go @@ -37,12 +37,26 @@ func (f *fakeObjects) ObjectsPutStream(ctx context.Context, _ string, r buscalls return buscalls.ObjectInfo{Name: r.Name, Size: uint64(len(data)), Description: description}, nil } -func (f *fakeObjects) ObjectsOpen(context.Context, string, buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) { - return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), true, nil +// ctxReader reads as the bus's object reader does: on the context it was opened on, failing once that is cancelled. +type ctxReader struct { + ctx context.Context + r io.Reader } -func (f *fakeObjects) SentOpen(context.Context, buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) { - return buscalls.ObjectInfo{Size: uint64(len(f.read))}, io.NopCloser(bytes.NewReader(f.read)), nil +func (c ctxReader) Read(p []byte) (int, error) { + if err := c.ctx.Err(); err != nil { + return 0, err + } + return c.r.Read(p) +} +func (c ctxReader) Close() error { return nil } + +func (f *fakeObjects) ObjectsOpen(ctx context.Context, _ string, _ buscalls.ObjectsNameRequest) (buscalls.ObjectInfo, io.ReadCloser, bool, error) { + return buscalls.ObjectInfo{Name: "o", Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, true, nil +} + +func (f *fakeObjects) SentOpen(ctx context.Context, _ buscalls.ObjectsSentGetRequest) (buscalls.ObjectInfo, io.ReadCloser, error) { + return buscalls.ObjectInfo{Size: uint64(len(f.read))}, ctxReader{ctx, bytes.NewReader(f.read)}, nil } func aSession(t *testing.T, f *fakeObjects) *moduleBus {