@@ -251,6 +251,7 @@ bool ContextBase::onStart(std::shared_ptr<PluginBase> plugin) {
251251 if (wasm_->on_context_create_ ) {
252252 plugin_ = plugin;
253253 wasm_->on_context_create_ (this , id_, 0 );
254+ in_vm_context_created_ = true ;
254255 plugin_.reset ();
255256 }
256257 if (wasm_->on_vm_start_ ) {
@@ -277,10 +278,14 @@ bool ContextBase::onConfigure(std::shared_ptr<PluginBase> plugin) {
277278}
278279
279280void ContextBase::onCreate (uint32_t parent_context_id) {
280- if (wasm_->on_context_create_ ) {
281+ if (!in_vm_context_created_ && wasm_->on_context_create_ ) {
281282 DeferAfterCallActions actions (this );
282283 wasm_->on_context_create_ (this , id_, parent_context_id);
284+ in_vm_context_created_ = true ;
283285 }
286+ // NB: If no on_context_create function is registered the in-VM SDK is responsible for
287+ // managing any required in-VM state.
288+ in_vm_context_created_ = true ;
284289}
285290
286291// Shared Data
@@ -337,11 +342,10 @@ void ContextBase::onTick(uint32_t) {
337342}
338343
339344FilterStatus ContextBase::onNetworkNewConnection () {
340- DeferAfterCallActions actions (this );
341- onCreate (root_context_id_);
342345 if (!wasm_->on_new_connection_ ) {
343346 return FilterStatus::Continue;
344347 }
348+ DeferAfterCallActions actions (this );
345349 if (wasm_->on_new_connection_ (this , id_).u64_ == 0 ) {
346350 return FilterStatus::Continue;
347351 }
@@ -388,12 +392,10 @@ void ContextBase::onUpstreamConnectionClose(CloseType close_type) {
388392template <typename P> static uint32_t headerSize (const P &p) { return p ? p->size () : 0 ; }
389393
390394FilterHeadersStatus ContextBase::onRequestHeaders (uint32_t headers, bool end_of_stream) {
391- DeferAfterCallActions actions (this );
392- onCreate (root_context_id_);
393- in_vm_context_created_ = true ;
394395 if (!wasm_->on_request_headers_ ) {
395396 return FilterHeadersStatus::Continue;
396397 }
398+ DeferAfterCallActions actions (this );
397399 auto result =
398400 wasm_->on_request_headers_ (this , id_, headers, static_cast <uint32_t >(end_of_stream)).u64_ ;
399401 if (result > static_cast <uint64_t >(FilterHeadersStatus::StopAllIterationAndWatermark))
@@ -438,18 +440,10 @@ FilterMetadataStatus ContextBase::onRequestMetadata(uint32_t elements) {
438440}
439441
440442FilterHeadersStatus ContextBase::onResponseHeaders (uint32_t headers, bool end_of_stream) {
441- DeferAfterCallActions actions (this );
442- if (!in_vm_context_created_) {
443- // If the request is invalid then onRequestHeaders() will not be called and neither will
444- // onCreate() then sendLocalReply be called which will call this function. In this case we
445- // need to call onCreate() so that the Context inside the VM is created before the
446- // onResponseHeaders() call.
447- onCreate (root_context_id_);
448- in_vm_context_created_ = true ;
449- }
450443 if (!wasm_->on_response_headers_ ) {
451444 return FilterHeadersStatus::Continue;
452445 }
446+ DeferAfterCallActions actions (this );
453447 auto result =
454448 wasm_->on_response_headers_ (this , id_, headers, static_cast <uint32_t >(end_of_stream)).u64_ ;
455449 if (result > static_cast <uint64_t >(FilterHeadersStatus::StopAllIterationAndWatermark))
@@ -542,23 +536,23 @@ void ContextBase::onGrpcClose(uint32_t token, uint32_t status_code) {
542536}
543537
544538bool ContextBase::onDone () {
545- DeferAfterCallActions actions (this );
546539 if (wasm_->on_done_ ) {
540+ DeferAfterCallActions actions (this );
547541 return wasm_->on_done_ (this , id_).u64_ != 0 ;
548542 }
549543 return true ;
550544}
551545
552546void ContextBase::onLog () {
553- DeferAfterCallActions actions (this );
554547 if (wasm_->on_log_ ) {
548+ DeferAfterCallActions actions (this );
555549 wasm_->on_log_ (this , id_);
556550 }
557551}
558552
559553void ContextBase::onDelete () {
560- DeferAfterCallActions actions ( this );
561- if (wasm_-> on_delete_ ) {
554+ if (in_vm_context_created_ && wasm_-> on_delete_ ) {
555+ DeferAfterCallActions actions ( this );
562556 wasm_->on_delete_ (this , id_);
563557 }
564558}
0 commit comments