Skip to content

Commit d9e0749

Browse files
authored
fix: moves the finalizer to guest creation. (#91)
1 parent 5881fad commit d9e0749

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

handler/middleware.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func NewMiddleware(ctx context.Context, guest []byte, host handler.Host, opts ..
112112
}
113113

114114
// Eagerly add one instance to the pool. Doing so helps to fail fast.
115-
if g, err := m.newGuest(ctx); err != nil {
115+
if g, err := m.getOrCreateGuest(ctx); err != nil {
116116
_ = wr.Close(ctx)
117117
return nil, err
118118
} else {
@@ -172,15 +172,20 @@ func (m *middleware) getOrCreateGuest(ctx context.Context) (*guest, error) {
172172
if g, createErr := m.newGuest(ctx); createErr != nil {
173173
return nil, createErr
174174
} else {
175+
// while closing the runtime will close the guest modules, when the pool
176+
// runs its own GC there are no guarantees that the guest module will be
177+
// closed and hence we need to ensure that the guest module is closed with
178+
// a finalizer.
175179
runtime.SetFinalizer(g, func(g *guest) {
176180
if err := g.guest.Close(context.Background()); err != nil {
177-
fmt.Printf("[http-wasm-host-go] middleware wazeroapi guest module close err: %v", err)
181+
m.logger.Log(ctx, api.LogLevelError, fmt.Sprintf("closing guest module: %v", err))
178182
} else {
179183
g.guest = nil
180184
g.handleRequestFn = nil
181185
g.handleResponseFn = nil
182186
}
183187
})
188+
184189
poolG = g
185190
}
186191
}

0 commit comments

Comments
 (0)