Skip to content

core: envoy engine#498

Merged
junr03 merged 11 commits intomasterfrom
ms/engine
Oct 17, 2019
Merged

core: envoy engine#498
junr03 merged 11 commits intomasterfrom
ms/engine

Conversation

@goaway
Copy link
Contributor

@goaway goaway commented Oct 11, 2019

Description: create a class to manage Envoy lifecycle and allow for graceful teardown. Teardown is now graceful for both iOS and Android. Envoy's codebase expects to have shutdown happen from the main thread. However, in mobile the thread the engine runs in is not the main thread of the process, and thus shutdown/destructors were not getting run from the thread the engine was started on. This PR makes sure that the engine is destructed/shutdown from the thread that it ran in.
Risk Level: high. This PR changes state management for the engine, and initializes it on a std::thread instead of a platform thread. iOS thread management is afaict handled gracefully. On Android the native thread has to be attached and detached from the JVM. This PR attaches the native thread, but the work to detach is a bit involved so will come in a subsequent PR.
Testing: local device testing.

Fixes #492

Co-authored-by: Jose Nino jnino@lyft.com
Signed-off-by: Mike Schore mike.schore@gmail.com

@goaway goaway changed the title wip core: envoy engine Oct 11, 2019
@goaway goaway force-pushed the ms/engine branch 6 times, most recently from dbee0ef to 9e77198 Compare October 11, 2019 21:23
Signed-off-by: Mike Schore <mike.schore@gmail.com>
Signed-off-by: Mike Schore <mike.schore@gmail.com>
goaway and others added 4 commits October 11, 2019 14:47
Signed-off-by: Mike Schore <mike.schore@gmail.com>
Signed-off-by: Mike Schore <mike.schore@gmail.com>
Signed-off-by: Mike Schore <mike.schore@gmail.com>
Signed-off-by: Jose Nino <jnino@lyft.com>
@junr03 junr03 marked this pull request as ready for review October 15, 2019 20:33
Jose Nino added 2 commits October 15, 2019 17:13
Signed-off-by: Jose Nino <jnino@lyft.com>
Signed-off-by: Jose Nino <jnino@lyft.com>
// Note: the only thread that should need to be attached is Envoy's engine std::thread.
// TODO: harden this piece of code to make sure that we are only needing to attach Envoy
// engine's std::thread, and that we detach it successfully.
static_jvm->AttachCurrentThread(&env, NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to add thread detachment in a subsequent PR, as it requires introducing engine level callbacks. I don't want to make this PR any bigger.

Jose Nino added 2 commits October 16, 2019 09:36
Signed-off-by: Jose Nino <jnino@lyft.com>
fmt
Signed-off-by: Jose Nino <jnino@lyft.com>
event_dispatcher_ = &main_common_->server()->dispatcher();
cv_.notifyOne();
} catch (const Envoy::NoServingException& e) {
return ENVOY_SUCCESS;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes NoServingException a successful run?

@junr03
Copy link
Member

junr03 commented Oct 16, 2019

@mattklein123 I am assigning you in case you can take a look, given mike (initially) and I (later) were the ones that wrote the code.

@goaway
Copy link
Contributor Author

goaway commented Oct 16, 2019

@junr03 just posting to say I think the updates you've made look good. 👍

@rebello95
Copy link
Contributor

Can we mention that this resolves #492 in the PR description and close out that issue when this merges? 😃

@junr03
Copy link
Member

junr03 commented Oct 16, 2019

Can we mention that this resolves #492 in the PR description and close out that issue when this merges? 😃

I wanted to to that in #507 as that completes the work for the crash 100%. It also completes work for #445. @rebello95 Does that sound good?

@rebello95
Copy link
Contributor

Sure, SGTM

Signed-off-by: Jose Nino <jnino@lyft.com>
@goaway
Copy link
Contributor Author

goaway commented Oct 16, 2019

Hm. I do think I'd suggest that this PR more directly addresses #492, and is probably the most relevant if we're ever looking for that commit.

@junr03
Copy link
Member

junr03 commented Oct 16, 2019

Sure, I can add it here.

Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM at a high level.

// Note: the only thread that should need to be attached is Envoy's engine std::thread.
// TODO: harden this piece of code to make sure that we are only needing to attach Envoy
// engine's std::thread, and that we detach it successfully.
static_jvm->AttachCurrentThread(&env, NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/NULL/nullptr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do in subsequent PR to avoid re-approval and re-running CI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junr03 junr03 merged commit 9133ddf into master Oct 17, 2019
junr03 added a commit that referenced this pull request Oct 17, 2019
Description: this is a follow up to #498. This PR introduces `envoy_engine_callbacks`. They are similar in nature to envoy_http_callbacks. The difference being that they are not exposed all the way to consumer level in the library as it is not needed right now. However, one can see how by adding a type erased context pointer, and following the platform patterns for http callbacks we could thread this all the way up if need be. The immediate need for these callbacks is to detach the engine's native thread from the JVM on Android.
Risk Level: med -- adds complexity to engine management.
Testing: local testing on devices (Lyft and example app on iOS and Android).

In conjunction with #498 this PR Fixes #492 #445

Signed-off-by: Jose Nino <jnino@lyft.com>
junr03 added a commit that referenced this pull request Oct 21, 2019
Description: #498 did not fully solve #492. The reset cleanly destructed all objects. However, because destruction was posted in to the event dispatcher, the event dispatcher was left with bad accesses.

This PR fixes the issue by issuing shutdown on the dispatcher, and only destructing once the event loop has exited and control has returned to the Engine's run function.

Risk Level: med - fixing crash on shutdown
Testing: local

Fixes #492

Signed-off-by: Jose Nino <jnino@lyft.com>
rebello95 added a commit that referenced this pull request Oct 21, 2019
Recent changes (namely #498) made it unnecessary for the platform-level clients to start Envoy on newly spawned threads. In fact, these changes actually introduced potential races where consumers of the platform clients could end up calling `send` on engines that hadn't finished starting on these threads.

This PR removes the custom threading logic in Swift in favor of simply using the new threading being done within the core. Another PR will make the same change on Android.

Signed-off-by: Michael Rebello <me@michaelrebello.com>
rebello95 added a commit that referenced this pull request Oct 21, 2019
Recent changes (namely #498) made it unnecessary for the platform-level clients to start Envoy on newly spawned threads. In fact, these changes actually introduced potential races where consumers of the platform clients could end up calling `send` on engines that hadn't finished starting on these threads.

This PR removes the custom threading logic in Swift in favor of simply using the new threading being done within the core. Another PR will make the same change on Android: #531.

Signed-off-by: Michael Rebello <me@michaelrebello.com>
junr03 added a commit that referenced this pull request Oct 22, 2019
Description: "Recent changes (namely #498) made it unnecessary for the platform-level clients to start Envoy on newly spawned threads. In fact, these changes actually introduced potential races where consumers of the platform clients could end up calling send on engines that hadn't finished starting on these threads."

This PR removes the custom threading logic in Kotlin in favor of simply using the new threading being done within the core. Parallel to #530.
Risk Level: med - reduce threading complexity
Testing: local app.

Signed-off-by: Jose Nino <jnino@lyft.com>
@goaway goaway deleted the ms/engine branch October 23, 2019 17:57
jpsim pushed a commit to envoyproxy/envoy that referenced this pull request Nov 28, 2022
Recent changes (namely envoyproxy/envoy-mobile#498) made it unnecessary for the platform-level clients to start Envoy on newly spawned threads. In fact, these changes actually introduced potential races where consumers of the platform clients could end up calling `send` on engines that hadn't finished starting on these threads.

This PR removes the custom threading logic in Swift in favor of simply using the new threading being done within the core. Another PR will make the same change on Android: envoyproxy/envoy-mobile#531.

Signed-off-by: Michael Rebello <me@michaelrebello.com>
Signed-off-by: JP Simard <jp@jpsim.com>
jpsim pushed a commit to envoyproxy/envoy that referenced this pull request Nov 29, 2022
Recent changes (namely envoyproxy/envoy-mobile#498) made it unnecessary for the platform-level clients to start Envoy on newly spawned threads. In fact, these changes actually introduced potential races where consumers of the platform clients could end up calling `send` on engines that hadn't finished starting on these threads.

This PR removes the custom threading logic in Swift in favor of simply using the new threading being done within the core. Another PR will make the same change on Android: envoyproxy/envoy-mobile#531.

Signed-off-by: Michael Rebello <me@michaelrebello.com>
Signed-off-by: JP Simard <jp@jpsim.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core/ios: crash when force-closing app with Envoy

5 participants