diff --git a/samples/shadow/mqtt5_shadow_sync/main.cpp b/samples/shadow/mqtt5_shadow_sync/main.cpp index b55b1d1cf..515da854c 100644 --- a/samples/shadow/mqtt5_shadow_sync/main.cpp +++ b/samples/shadow/mqtt5_shadow_sync/main.cpp @@ -311,6 +311,7 @@ int main(int argc, char *argv[]) std::promise subscribeGetShadowRejectedCompletedPromise; std::promise onGetShadowRequestCompletedPromise; std::promise gotInitialShadowPromise; + bool isInitialShadowReceived = false; auto onGetShadowUpdatedAcceptedSubAck = [&](int ioErr) { if (ioErr != AWS_OP_SUCCESS) @@ -347,6 +348,15 @@ int main(int argc, char *argv[]) } if (response) { + // If another client requested shadow for the same thing at the same time, this callback might be + // triggered more than once. Ignore everything after first data arrived. + if (isInitialShadowReceived) + { + fprintf(stderr, "Initial shadow is already set, ignore\n"); + return; + } + isInitialShadowReceived = true; + fprintf(stdout, "Received shadow document.\n"); if (response->State && response->State->Reported->View().ValueExists(cmdData.input_shadowProperty)) { @@ -382,6 +392,15 @@ int main(int argc, char *argv[]) fprintf(stderr, "Error on getting shadow document: %s.\n", ErrorDebugString(ioErr)); exit(-1); } + // If another client requested shadow for the same thing at the same time, this callback might be + // triggered more than once. Ignore everything after first data arrived. + if (isInitialShadowReceived) + { + fprintf(stderr, "Initial shadow is already set, ignore\n"); + return; + } + isInitialShadowReceived = true; + fprintf( stdout, "Getting shadow document failed with message %s and code %d.\n", diff --git a/samples/shadow/shadow_sync/main.cpp b/samples/shadow/shadow_sync/main.cpp index 234a58e42..3eec289d1 100644 --- a/samples/shadow/shadow_sync/main.cpp +++ b/samples/shadow/shadow_sync/main.cpp @@ -340,6 +340,7 @@ int main(int argc, char *argv[]) std::promise subscribeGetShadowRejectedCompletedPromise; std::promise onGetShadowRequestCompletedPromise; std::promise gotInitialShadowPromise; + bool isInitialShadowReceived = false; auto onGetShadowUpdatedAcceptedSubAck = [&](int ioErr) { if (ioErr != AWS_OP_SUCCESS) @@ -376,6 +377,15 @@ int main(int argc, char *argv[]) } if (response) { + // If another client requested shadow for the same thing at the same time, this callback might be + // triggered more than once. Ignore everything after first data arrived. + if (isInitialShadowReceived) + { + fprintf(stderr, "Initial shadow is already set, ignore\n"); + return; + } + isInitialShadowReceived = true; + fprintf(stdout, "Received shadow document.\n"); if (response->State && response->State->Reported->View().ValueExists(cmdData.input_shadowProperty)) { @@ -411,6 +421,15 @@ int main(int argc, char *argv[]) fprintf(stderr, "Error on getting shadow document: %s.\n", ErrorDebugString(ioErr)); exit(-1); } + // If another client requested shadow for the same thing at the same time, this callback might be + // triggered more than once. Ignore everything after first data arrived. + if (isInitialShadowReceived) + { + fprintf(stderr, "Initial shadow is already set, ignore\n"); + return; + } + isInitialShadowReceived = true; + fprintf( stdout, "Getting shadow document failed with message %s and code %d.\n",