Skip to content

Commit 6c1fc57

Browse files
authored
Continuation activation race condition fix (#342)
* Pull out the future before initiating async operations, otherwise the promise may already be destroyed by the time you try and pull out the future
1 parent 7475c61 commit 6c1fc57

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

eventstream_rpc/source/EventStreamClient.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,15 @@ namespace Aws
966966
int errorCode =
967967
EventStreamCppToNativeCrtBuilder::s_fillNativeHeadersArray(headers, &headersArray, m_allocator);
968968

969+
/*
970+
* Regardless of how the promise gets moved around (or not), this future should stay valid as a return
971+
* value.
972+
*
973+
* We pull it out early because the call to aws_event_stream_rpc_client_continuation_activate() may complete
974+
* and delete the promise before we pull out the future afterwords.
975+
*/
976+
std::future<RpcError> retValue = onFlushPromise.get_future();
977+
969978
if (!errorCode)
970979
{
971980
struct aws_event_stream_rpc_message_args msg_args;
@@ -1001,12 +1010,8 @@ namespace Aws
10011010
onFlushPromise.set_value({EVENT_STREAM_RPC_CRT_ERROR, errorCode});
10021011
Crt::Delete(callbackContainer, m_allocator);
10031012
}
1004-
else
1005-
{
1006-
return callbackContainer->onFlushPromise.get_future();
1007-
}
10081013

1009-
return onFlushPromise.get_future();
1014+
return retValue;
10101015
}
10111016

10121017
std::future<RpcError> ClientContinuation::SendMessage(
@@ -1588,4 +1593,4 @@ namespace Aws
15881593
}
15891594

15901595
} /* namespace Eventstreamrpc */
1591-
} // namespace Aws
1596+
} // namespace Aws

0 commit comments

Comments
 (0)