From 68759712d7bf67202d437afdab75b7ee92657659 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 13 Nov 2020 11:01:32 -0800 Subject: [PATCH] Roll forward to cl/342290621 Signed-off-by: hai007 --- cpp/core/internal/base_endpoint_channel.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/core/internal/base_endpoint_channel.cc b/cpp/core/internal/base_endpoint_channel.cc index 735cf6e2..26184958 100644 --- a/cpp/core/internal/base_endpoint_channel.cc +++ b/cpp/core/internal/base_endpoint_channel.cc @@ -149,7 +149,12 @@ Exception BaseEndpointChannel::Write(const ByteArray& data) { ByteArray encrypted_data; const ByteArray* data_to_write = &data; { + // Holding both mutexes is necessary to prevent the keep alive and payload + // threads from writing encrypted messages out of order which causes a + // failure to decrypt on the reader side. MutexLock crypto_lock(&crypto_mutex_); + MutexLock lock(&writer_mutex_); + if (IsEncryptionEnabledLocked()) { // If encryption is enabled, encode the message. std::unique_ptr encrypted = @@ -158,10 +163,7 @@ Exception BaseEndpointChannel::Write(const ByteArray& data) { encrypted_data = ByteArray(std::move(*encrypted)); data_to_write = &encrypted_data; } - } - { - MutexLock lock(&writer_mutex_); Exception write_exception = WriteInt(writer_, static_cast(data_to_write->size())); if (write_exception.Raised()) {