@@ -327,6 +327,8 @@ ngx_proxy_wasm_ctx(ngx_uint_t *filter_ids, size_t nfilters,
327
327
pwexec -> pool = pool ;
328
328
pwexec -> filter = filter ;
329
329
pwexec -> parent = pwctx ;
330
+ pwexec -> root_id = filter -> id ;
331
+
330
332
pwexec -> log = ngx_pcalloc (pwexec -> pool , sizeof (ngx_log_t ));
331
333
if (pwexec -> log == NULL ) {
332
334
return NULL ;
@@ -344,14 +346,12 @@ ngx_proxy_wasm_ctx(ngx_uint_t *filter_ids, size_t nfilters,
344
346
pwexec -> log_ctx .pwexec = pwexec ;
345
347
pwexec -> log_ctx .orig_log = log ;
346
348
347
- ictx = ngx_proxy_wasm_get_instance (filter , pwstore , log );
349
+ ictx = ngx_proxy_wasm_get_instance (filter , pwstore , pwexec , log );
348
350
if (ictx == NULL ) {
349
351
return NULL ;
350
352
}
351
353
352
354
pwexec -> ictx = ictx ;
353
- pwexec -> id = ictx -> next_id ++ ;
354
- pwexec -> root_id = filter -> id ;
355
355
356
356
ngx_wasm_assert (pwexec -> index + 1 == pwctx -> pwexecs .nelts );
357
357
@@ -461,7 +461,7 @@ ngx_proxy_wasm_action2rc(ngx_proxy_wasm_ctx_t *pwctx,
461
461
filter = pwexec -> filter ;
462
462
action = pwctx -> action ;
463
463
464
- dd ("action: %d" , action );
464
+ dd ("enter (pwexec: %p, action: %d)" , pwexec , action );
465
465
466
466
if (pwexec -> ecode ) {
467
467
if (!pwexec -> ecode_logged
@@ -611,7 +611,6 @@ ngx_proxy_wasm_run_step(ngx_proxy_wasm_exec_t *pwexec,
611
611
ngx_proxy_wasm_instance_t * ictx , ngx_proxy_wasm_step_e step )
612
612
{
613
613
ngx_int_t rc ;
614
- ngx_proxy_wasm_err_e ecode ;
615
614
ngx_proxy_wasm_action_e action = NGX_PROXY_WASM_ACTION_CONTINUE ;
616
615
ngx_proxy_wasm_ctx_t * pwctx = pwexec -> parent ;
617
616
ngx_proxy_wasm_filter_t * filter = pwexec -> filter ;
@@ -657,12 +656,6 @@ ngx_proxy_wasm_run_step(ngx_proxy_wasm_exec_t *pwexec,
657
656
#ifdef NGX_WASM_RESPONSE_TRAILERS
658
657
case NGX_PROXY_WASM_STEP_RESP_TRAILERS :
659
658
#endif
660
- ecode = ngx_proxy_wasm_on_start (ictx , filter , 0 );
661
- if (ecode != NGX_PROXY_WASM_ERR_NONE ) {
662
- pwexec -> ecode = ecode ;
663
- goto done ;
664
- }
665
-
666
659
rc = filter -> subsystem -> resume (pwexec , step , & action );
667
660
break ;
668
661
case NGX_PROXY_WASM_STEP_LOG :
@@ -1177,7 +1170,6 @@ ngx_proxy_wasm_init_abi(ngx_proxy_wasm_filter_t *filter)
1177
1170
static ngx_int_t
1178
1171
ngx_proxy_wasm_start_filter (ngx_proxy_wasm_filter_t * filter )
1179
1172
{
1180
- ngx_proxy_wasm_err_e ecode ;
1181
1173
ngx_proxy_wasm_instance_t * ictx ;
1182
1174
1183
1175
ngx_wasm_assert (filter -> loaded );
@@ -1190,24 +1182,12 @@ ngx_proxy_wasm_start_filter(ngx_proxy_wasm_filter_t *filter)
1190
1182
return NGX_OK ;
1191
1183
}
1192
1184
1193
- ictx = ngx_proxy_wasm_get_instance (filter , filter -> store , filter -> log );
1185
+ ictx = ngx_proxy_wasm_get_instance (filter , filter -> store , NULL ,
1186
+ filter -> log );
1194
1187
if (ictx == NULL ) {
1195
1188
return NGX_ERROR ;
1196
1189
}
1197
1190
1198
- /*
1199
- * update instance log
1200
- * FFI-injected filters have a valid log while the instance's
1201
- * might be outdated.
1202
- */
1203
- ngx_wavm_instance_set_data (ictx -> instance , ictx , filter -> log );
1204
-
1205
- ecode = ngx_proxy_wasm_on_start (ictx , filter , 1 );
1206
- if (ecode != NGX_PROXY_WASM_ERR_NONE ) {
1207
- filter -> ecode = ecode ;
1208
- return NGX_ERROR ;
1209
- }
1210
-
1211
1191
filter -> started = 1 ;
1212
1192
1213
1193
return NGX_OK ;
@@ -1216,12 +1196,16 @@ ngx_proxy_wasm_start_filter(ngx_proxy_wasm_filter_t *filter)
1216
1196
1217
1197
ngx_proxy_wasm_instance_t *
1218
1198
ngx_proxy_wasm_get_instance (ngx_proxy_wasm_filter_t * filter ,
1219
- ngx_proxy_wasm_store_t * store , ngx_log_t * log )
1199
+ ngx_proxy_wasm_store_t * store , ngx_proxy_wasm_exec_t * pwexec ,
1200
+ ngx_log_t * log )
1220
1201
{
1221
1202
ngx_queue_t * q ;
1222
1203
ngx_pool_t * pool ;
1223
1204
ngx_wavm_module_t * module = filter -> module ;
1224
- ngx_proxy_wasm_instance_t * ictx = NULL ;
1205
+ ngx_proxy_wasm_instance_t * ictx ;
1206
+ ngx_proxy_wasm_err_e ecode ;
1207
+
1208
+ dd ("enter (pwexec: %p)" , pwexec );
1225
1209
1226
1210
if (store == NULL ) {
1227
1211
dd ("no store, jump to create" );
@@ -1250,7 +1234,7 @@ ngx_proxy_wasm_get_instance(ngx_proxy_wasm_filter_t *filter,
1250
1234
}
1251
1235
1252
1236
if (ictx -> module == filter -> module ) {
1253
- dd ("reuse instance" );
1237
+ dd ("reuse busy instance" );
1254
1238
ngx_wasm_assert (ictx -> nrefs );
1255
1239
goto reuse ;
1256
1240
}
@@ -1262,17 +1246,29 @@ ngx_proxy_wasm_get_instance(ngx_proxy_wasm_filter_t *filter,
1262
1246
{
1263
1247
ictx = ngx_queue_data (q , ngx_proxy_wasm_instance_t , q );
1264
1248
1249
+ if (ictx -> instance -> trapped ) {
1250
+ ngx_proxy_wasm_log_error (NGX_LOG_DEBUG , log , 0 ,
1251
+ "\"%V\" filter freeing trapped instance "
1252
+ "(ictx: %p, store: %p)" ,
1253
+ filter -> name , ictx , store );
1254
+
1255
+ q = ngx_queue_next (& ictx -> q );
1256
+ ngx_proxy_wasm_release_instance (ictx , 1 );
1257
+ continue ;
1258
+ }
1259
+
1265
1260
if (ictx -> module == filter -> module ) {
1261
+ dd ("reuse free instance" );
1266
1262
ngx_wasm_assert (ictx -> nrefs == 0 );
1267
1263
ngx_queue_remove (& ictx -> q );
1268
1264
goto reuse ;
1269
1265
}
1270
1266
}
1271
1267
1272
- dd ("create instance in store: %p" , store );
1273
-
1274
1268
create :
1275
1269
1270
+ dd ("create instance in store: %p" , store );
1271
+
1276
1272
ictx = ngx_pcalloc (pool , sizeof (ngx_proxy_wasm_instance_t ));
1277
1273
if (ictx == NULL ) {
1278
1274
goto error ;
@@ -1302,26 +1298,63 @@ ngx_proxy_wasm_get_instance(ngx_proxy_wasm_filter_t *filter,
1302
1298
1303
1299
goto done ;
1304
1300
1305
- error :
1306
-
1307
- return NULL ;
1308
-
1309
1301
reuse :
1310
1302
1311
1303
ngx_proxy_wasm_log_error (NGX_LOG_DEBUG , log , 0 ,
1312
1304
"\"%V\" filter reusing instance "
1313
1305
"(ictx: %p, nrefs: %d, store: %p)" ,
1314
1306
filter -> name , ictx , ictx -> nrefs + 1 , store );
1315
1307
1308
+ goto done ;
1309
+
1316
1310
done :
1317
1311
1318
1312
if (store && !ictx -> nrefs ) {
1319
1313
ngx_queue_insert_tail (& store -> busy , & ictx -> q );
1320
1314
}
1321
1315
1316
+ if (pwexec ) {
1317
+ if (pwexec -> root_id != NGX_PROXY_WASM_ROOT_CTX_ID ) {
1318
+ ngx_wasm_assert (pwexec -> id == 0 );
1319
+ pwexec -> id = ictx -> next_id ++ ;
1320
+ }
1321
+
1322
+ if (pwexec -> ecode ) {
1323
+ /* recycled instance */
1324
+ pwexec -> ecode = NGX_PROXY_WASM_ERR_NONE ;
1325
+ pwexec -> ecode_logged = 0 ;
1326
+ }
1327
+ }
1328
+
1322
1329
ictx -> nrefs ++ ;
1330
+ ictx -> pwexec = pwexec ;
1331
+
1332
+ /**
1333
+ * update instance log
1334
+ * FFI-injected filters have a valid log while the instance's
1335
+ * might be outdated.
1336
+ */
1337
+ ngx_wavm_instance_set_data (ictx -> instance , ictx , log );
1338
+
1339
+ /* create wasm context (root/http) */
1340
+
1341
+ ecode = ngx_proxy_wasm_on_start (ictx , filter , pwexec == NULL );
1342
+ if (ecode != NGX_PROXY_WASM_ERR_NONE ) {
1343
+ if (pwexec ) {
1344
+ pwexec -> ecode = ecode ;
1345
+
1346
+ } else {
1347
+ filter -> ecode = ecode ;
1348
+ }
1349
+
1350
+ goto error ;
1351
+ }
1323
1352
1324
1353
return ictx ;
1354
+
1355
+ error :
1356
+
1357
+ return NULL ;
1325
1358
}
1326
1359
1327
1360
@@ -1421,6 +1454,8 @@ ngx_proxy_wasm_on_start(ngx_proxy_wasm_instance_t *ictx,
1421
1454
ngx_wavm_instance_t * instance = ictx -> instance ;
1422
1455
wasm_val_vec_t * rets ;
1423
1456
1457
+ dd ("enter (pwexec: %p, ictx: %p)" , pwexec , ictx );
1458
+
1424
1459
rexec = ngx_proxy_wasm_root_lookup (ictx , filter -> id );
1425
1460
if (rexec == NULL ) {
1426
1461
rexec = ngx_pcalloc (ictx -> pool , sizeof (ngx_proxy_wasm_exec_t ));
0 commit comments