Skip to content

Commit 424ed0e

Browse files
committed
Fix iOS termination crash
When iOS is terminating a process, it may release some static memory, which could cause the `event_dispatcher_` variable to point to invalid memory, leading to a crash. A crash that occurs when an app is already in the process of being terminated isn't too bad, considering it's already shutting down, but this matters to some extent, considering that crash reporters will report this as an app crash, and the OS may choose to penalize your app by not proactively suggesting it. This should fix a crash with a backtrace such as this one: ``` EXC_BAD_ACCESS: Attempted to dereference garbage pointer 0x8000000000000010. 0 Envoy::Event::ProvisionalDispatcher::post(std::__1::function<void ()>) 1 Envoy::EngineHandle::runOnEngineDispatcher(long, std::__1::function<void (Envoy::Engine&)>) 2 start_stream 3 -[EnvoyHTTPStreamImpl initWithHandle:callbacks:explicitFlowControl:] 4 -[EnvoyEngineImpl startStreamWithCallbacks:explicitFlowControl:] 5 StreamPrototype.start(queue:) ``` Signed-off-by: JP Simard <[email protected]>
1 parent fa00fc7 commit 424ed0e

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

library/cc/engine.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ void Engine::terminate() {
2626
terminated_ = true;
2727
}
2828

29+
bool Engine::isTerminated() { return terminated_; }
30+
2931
} // namespace Platform
3032
} // namespace Envoy

library/cc/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Engine : public std::enable_shared_from_this<Engine> {
1919
PulseClientSharedPtr pulseClient();
2020

2121
void terminate();
22+
bool isTerminated();
2223

2324
private:
2425
Engine(envoy_engine_t engine);

library/common/engine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class Engine : public Logger::Loggable<Logger::Id::main> {
4242
*/
4343
envoy_status_t terminate();
4444

45+
/**
46+
* Whether this engine has been terminated.
47+
*/
48+
bool isTerminated();
49+
4550
/**
4651
* Accessor for the provisional event dispatcher.
4752
* @return Event::ProvisionalDispatcher&, the engine dispatcher.

library/common/engine_handle.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace Envoy {
55
envoy_status_t EngineHandle::runOnEngineDispatcher(envoy_engine_t,
66
std::function<void(Envoy::Engine&)> func) {
77
if (auto e = engine()) {
8+
if (e->isTerminated()) {
9+
return ENVOY_FAILURE;
10+
}
11+
812
return e->dispatcher().post([func]() {
913
if (auto e = engine()) {
1014
func(*e);

0 commit comments

Comments
 (0)