Skip to content

Commit f5e250b

Browse files
committed
AppV2: ensure isolation of handler state per request
Implemented a new duplication mechanism for the handler to create separate instances per request. This change addresses an issue where the same handler instance was shared across multiple requests, leading to state leakage and inconsistencies in AppV2. Each request now uses a fresh handler instance, ensuring isolated and consistent state management. Updates goplus/builder#532 Signed-off-by: Aofei Sheng <[email protected]>
1 parent 72ad5b0 commit f5e250b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

classfile_v2.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,22 @@ type iHandler interface {
5757
func Gopt_AppV2_Main(app AppType, handlers ...iHandler) {
5858
app.InitYap()
5959
for _, h := range handlers {
60-
reflect.ValueOf(h).Elem().Field(1).Set(reflect.ValueOf(app)) // (*handler).AppV2 = app
60+
hVal := reflect.ValueOf(h).Elem()
61+
hVal.FieldByName("AppV2").Set(reflect.ValueOf(app))
62+
hType := hVal.Type()
63+
handle := func(ctx *Context) {
64+
// We must duplicate the handler instance for each request
65+
// to ensure state isolation.
66+
h2Val := reflect.New(hType).Elem()
67+
h2Val.Set(hVal)
68+
h2 := h2Val.Addr().Interface().(iHandler)
69+
h2.Main(ctx)
70+
}
6171
switch method, path := parseClassfname(h.Classfname()); method {
6272
case "handle":
63-
app.Handle(path, h.Main)
73+
app.Handle(path, handle)
6474
default:
65-
app.Route(strings.ToUpper(method), path, h.Main)
75+
app.Route(strings.ToUpper(method), path, handle)
6676
}
6777
}
6878
if me, ok := app.(interface{ MainEntry() }); ok {

0 commit comments

Comments
 (0)