Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/cbang/ws/Websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ void Websocket::connect(HTTP::Client &client, const URI &uri) {
start();

} else {
// Release refs first so a reconnect from onClose() is not clobbered
outConn.release();
connection.release();
onClose(WS_STATUS_NONE,
SSTR("Connection failed: error=" << error << " code=" << code));
connection.release();
}
};

Expand All @@ -88,7 +90,11 @@ void Websocket::connect(HTTP::Client &client, const URI &uri) {
req->outSet("Upgrade", "websocket");
req->outSet("Connection", "upgrade");

connection = client.send(req);
// Hold a strong reference to the outgoing connection; other refs are weak
outConn = client.send(req);
connection = outConn;
// Cache ID like upgrade() does, since the weak ref may not outlive us
id = outConn->getID();
}


Expand Down Expand Up @@ -439,10 +445,10 @@ void Websocket::shutdown() {
SmartPointer<Websocket> self = this;

SmartPointer<HTTP::Conn> conn = connection;
if (conn.isSet()) {
connection.release();
conn->close();
}
// Clear members first; conn->close() callbacks may re-enter this class
outConn.release();
connection.release();
if (conn.isSet()) conn->close();

if (active) {
active = false;
Expand Down
1 change: 1 addition & 0 deletions src/cbang/ws/Websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace cb {
namespace WS {
class Websocket : virtual public RefCounted, public Enum {
SmartPointer<HTTP::Conn>::Weak connection;
SmartPointer<HTTP::Conn> outConn;
uint64_t id = ~0;
bool active = false;

Expand Down