Skip to content

Commit ac3ba37

Browse files
Secure tunneling stop (#449)
* Shutdown crash workaround; long term followup rework C impl semantics to match broader async correctness/safety/patterns Co-authored-by: Marco Morais <[email protected]>
1 parent 93cee21 commit ac3ba37

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

secure_tunneling/include/aws/iotsecuretunneling/SecureTunnel.h

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <aws/iotdevice/secure_tunneling.h>
1111
#include <aws/iotsecuretunneling/Exports.h>
1212

13+
#include <future>
14+
1315
namespace Aws
1416
{
1517
namespace Iotsecuretunneling
@@ -153,6 +155,8 @@ namespace Aws
153155

154156
int Close();
155157

158+
void Shutdown();
159+
156160
int SendData(const Crt::ByteCursor &data);
157161

158162
int SendStreamStart();
@@ -193,6 +197,9 @@ namespace Aws
193197
static void s_OnStreamStart(void *user_data);
194198
static void s_OnStreamReset(void *user_data);
195199
static void s_OnSessionReset(void *user_data);
200+
static void s_OnTerminationComplete(void *user_data);
201+
202+
void OnTerminationComplete();
196203

197204
// Client callbacks
198205
OnConnectionComplete m_OnConnectionComplete;
@@ -204,6 +211,8 @@ namespace Aws
204211
OnSessionReset m_OnSessionReset;
205212
aws_secure_tunnel *m_secure_tunnel;
206213

214+
std::promise<void> m_TerminationComplete;
215+
207216
friend class SecureTunnelBuilder;
208217
};
209218
} // namespace Iotsecuretunneling

secure_tunneling/source/SecureTunnel.cpp

+49-28
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,43 @@ namespace Aws
5353

5454
SecureTunnelBuilder &SecureTunnelBuilder::WithOnConnectionComplete(OnConnectionComplete onConnectionComplete)
5555
{
56-
m_OnConnectionComplete = onConnectionComplete;
56+
m_OnConnectionComplete = std::move(onConnectionComplete);
5757
return *this;
5858
}
5959

6060
SecureTunnelBuilder &SecureTunnelBuilder::WithOnConnectionShutdown(OnConnectionShutdown onConnectionShutdown)
6161
{
62-
m_OnConnectionShutdown = onConnectionShutdown;
62+
m_OnConnectionShutdown = std::move(onConnectionShutdown);
6363
return *this;
6464
}
6565

6666
SecureTunnelBuilder &SecureTunnelBuilder::WithOnSendDataComplete(OnSendDataComplete onSendDataComplete)
6767
{
68-
m_OnSendDataComplete = onSendDataComplete;
68+
m_OnSendDataComplete = std::move(onSendDataComplete);
6969
return *this;
7070
}
7171

7272
SecureTunnelBuilder &SecureTunnelBuilder::WithOnDataReceive(OnDataReceive onDataReceive)
7373
{
74-
m_OnDataReceive = onDataReceive;
74+
m_OnDataReceive = std::move(onDataReceive);
7575
return *this;
7676
}
7777

7878
SecureTunnelBuilder &SecureTunnelBuilder::WithOnStreamStart(OnStreamStart onStreamStart)
7979
{
80-
m_OnStreamStart = onStreamStart;
80+
m_OnStreamStart = std::move(onStreamStart);
8181
return *this;
8282
}
8383

8484
SecureTunnelBuilder &SecureTunnelBuilder::WithOnStreamReset(OnStreamReset onStreamReset)
8585
{
86-
m_OnStreamReset = onStreamReset;
86+
m_OnStreamReset = std::move(onStreamReset);
8787
return *this;
8888
}
8989

9090
SecureTunnelBuilder &SecureTunnelBuilder::WithOnSessionReset(OnSessionReset onSessionReset)
9191
{
92-
m_OnSessionReset = onSessionReset;
92+
m_OnSessionReset = std::move(onSessionReset);
9393
return *this;
9494
}
9595

@@ -144,13 +144,13 @@ namespace Aws
144144
OnSessionReset onSessionReset)
145145
{
146146
// Client callbacks
147-
m_OnConnectionComplete = onConnectionComplete;
148-
m_OnConnectionShutdown = onConnectionShutdown;
149-
m_OnSendDataComplete = onSendDataComplete;
150-
m_OnDataReceive = onDataReceive;
151-
m_OnStreamStart = onStreamStart;
152-
m_OnStreamReset = onStreamReset;
153-
m_OnSessionReset = onSessionReset;
147+
m_OnConnectionComplete = std::move(onConnectionComplete);
148+
m_OnConnectionShutdown = std::move(onConnectionShutdown);
149+
m_OnSendDataComplete = std::move(onSendDataComplete);
150+
m_OnDataReceive = std::move(onDataReceive);
151+
m_OnStreamStart = std::move(onStreamStart);
152+
m_OnStreamReset = std::move(onStreamReset);
153+
m_OnSessionReset = std::move(onSessionReset);
154154

155155
// Initialize aws_secure_tunnel_options
156156
aws_secure_tunnel_options config;
@@ -176,6 +176,7 @@ namespace Aws
176176
config.on_stream_start = s_OnStreamStart;
177177
config.on_stream_reset = s_OnStreamReset;
178178
config.on_session_reset = s_OnSessionReset;
179+
config.on_termination_complete = s_OnTerminationComplete;
179180

180181
config.user_data = this;
181182

@@ -272,13 +273,15 @@ namespace Aws
272273

273274
SecureTunnel::SecureTunnel(SecureTunnel &&other) noexcept
274275
{
275-
m_OnConnectionComplete = other.m_OnConnectionComplete;
276-
m_OnConnectionShutdown = other.m_OnConnectionShutdown;
277-
m_OnSendDataComplete = other.m_OnSendDataComplete;
278-
m_OnDataReceive = other.m_OnDataReceive;
279-
m_OnStreamStart = other.m_OnStreamStart;
280-
m_OnStreamReset = other.m_OnStreamReset;
281-
m_OnSessionReset = other.m_OnSessionReset;
276+
m_OnConnectionComplete = std::move(other.m_OnConnectionComplete);
277+
m_OnConnectionShutdown = std::move(other.m_OnConnectionShutdown);
278+
m_OnSendDataComplete = std::move(other.m_OnSendDataComplete);
279+
m_OnDataReceive = std::move(other.m_OnDataReceive);
280+
m_OnStreamStart = std::move(other.m_OnStreamStart);
281+
m_OnStreamReset = std::move(other.m_OnStreamReset);
282+
m_OnSessionReset = std::move(other.m_OnSessionReset);
283+
284+
m_TerminationComplete = std::move(other.m_TerminationComplete);
282285

283286
m_secure_tunnel = other.m_secure_tunnel;
284287

@@ -299,13 +302,15 @@ namespace Aws
299302
{
300303
this->~SecureTunnel();
301304

302-
m_OnConnectionComplete = other.m_OnConnectionComplete;
303-
m_OnConnectionShutdown = other.m_OnConnectionShutdown;
304-
m_OnSendDataComplete = other.m_OnSendDataComplete;
305-
m_OnDataReceive = other.m_OnDataReceive;
306-
m_OnStreamStart = other.m_OnStreamStart;
307-
m_OnStreamReset = other.m_OnStreamReset;
308-
m_OnSessionReset = other.m_OnSessionReset;
305+
m_OnConnectionComplete = std::move(other.m_OnConnectionComplete);
306+
m_OnConnectionShutdown = std::move(other.m_OnConnectionShutdown);
307+
m_OnSendDataComplete = std::move(other.m_OnSendDataComplete);
308+
m_OnDataReceive = std::move(other.m_OnDataReceive);
309+
m_OnStreamStart = std::move(other.m_OnStreamStart);
310+
m_OnStreamReset = std::move(other.m_OnStreamReset);
311+
m_OnSessionReset = std::move(other.m_OnSessionReset);
312+
313+
m_TerminationComplete = std::move(other.m_TerminationComplete);
309314

310315
m_secure_tunnel = other.m_secure_tunnel;
311316

@@ -395,5 +400,21 @@ namespace Aws
395400
}
396401
}
397402

403+
void SecureTunnel::s_OnTerminationComplete(void *user_data)
404+
{
405+
auto *secureTunnel = static_cast<SecureTunnel *>(user_data);
406+
secureTunnel->OnTerminationComplete();
407+
}
408+
409+
void SecureTunnel::OnTerminationComplete() { m_TerminationComplete.set_value(); }
410+
411+
void SecureTunnel::Shutdown()
412+
{
413+
Close();
414+
aws_secure_tunnel_release(m_secure_tunnel);
415+
m_secure_tunnel = nullptr;
416+
417+
m_TerminationComplete.get_future().wait();
418+
}
398419
} // namespace Iotsecuretunneling
399420
} // namespace Aws

secure_tunneling/tests/SecureTunnelTest.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static int after(struct aws_allocator *allocator, int setup_result, void *ctx)
109109
{
110110
auto *testContext = static_cast<SecureTunnelingTestContext *>(ctx);
111111

112+
testContext->secureTunnel->Shutdown();
113+
112114
testContext->secureTunnel.reset();
113115
testContext->clientBootstrap.reset();
114116
testContext->resolver.reset();

0 commit comments

Comments
 (0)