Skip to content

Commit

Permalink
chore: prevent Dispatch fiber to be launched during migration (#3123)
Browse files Browse the repository at this point in the history
* chore: prevent Dispatch fiber to be launched during the connection migration
  • Loading branch information
romange authored Jun 4, 2024
1 parent 5b731f1 commit b02521c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
30 changes: 23 additions & 7 deletions src/facade/dragonfly_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ Connection::Connection(Protocol protocol, util::HttpListenerBase* http_listener,
http_listener_(http_listener),
ssl_ctx_(ctx),
service_(service),
name_{} {
tracking_enabled_(false),
skip_next_squashing_(false),
migration_enabled_(false),
migration_in_process_(false),
is_http_(false) {
static atomic_uint32_t next_id{1};

protocol_ = protocol;
Expand Down Expand Up @@ -556,22 +560,33 @@ void Connection::OnShutdown() {
}

void Connection::OnPreMigrateThread() {
DVLOG(1) << "OnPreMigrateThread " << GetClientId();

CHECK(!cc_->conn_closing);

DCHECK(!migration_in_process_);

// CancelOnErrorCb is a preemption point, so we make sure the Migration start
// is marked beforehand.
migration_in_process_ = true;

socket_->CancelOnErrorCb();
DCHECK(!dispatch_fb_.IsJoinable()) << GetClientId();
}

void Connection::OnPostMigrateThread() {
DVLOG(1) << "OnPostMigrateThread " << GetClientId();

// Once we migrated, we should rearm OnBreakCb callback.
if (breaker_cb_ && socket()->IsOpen()) {
socket_->RegisterOnErrorCb([this](int32_t mask) { this->OnBreakCb(mask); });
}
migration_in_process_ = false;
DCHECK(!dispatch_fb_.IsJoinable());

// If someone had sent Async during the migration, dispatch_fb_ will be created.
if (dispatch_fb_.IsJoinable()) {
// How can we ensure that dispatch_fb_ is created on the correct thread?
// TODO: to introduce Fiber::IsLocal method.
DCHECK(!dispatch_q_.empty());
// If someone had sent Async during the migration, we must create dispatch_fb_.
if (!dispatch_q_.empty()) {
LaunchDispatchFiberIfNeeded();
}

// Update tl variables
Expand Down Expand Up @@ -1530,7 +1545,8 @@ void Connection::SendInvalidationMessageAsync(InvalidationMessage msg) {
}

void Connection::LaunchDispatchFiberIfNeeded() {
if (!dispatch_fb_.IsJoinable()) {
if (!dispatch_fb_.IsJoinable() && !migration_in_process_) {
VLOG(1) << "LaunchDispatchFiberIfNeeded " << GetClientId();
dispatch_fb_ = fb2::Fiber(fb2::Launch::post, "connection_dispatch",
[&, peer = socket_.get()]() { DispatchFiber(peer); });
}
Expand Down
9 changes: 5 additions & 4 deletions src/facade/dragonfly_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,13 @@ class Connection : public util::Connection {
static thread_local QueueBackpressure tl_queue_backpressure_;

// a flag indicating whether the client has turned on client tracking.
bool tracking_enabled_ = false;
bool skip_next_squashing_ = false; // Forcefully skip next squashing
bool tracking_enabled_ : 1;
bool skip_next_squashing_ : 1; // Forcefully skip next squashing

// Connection migration vars, see RequestAsyncMigration() above.
bool migration_enabled_ = false;
bool is_http_ = false;
bool migration_enabled_ : 1;
bool migration_in_process_ : 1;
bool is_http_ : 1;
};

} // namespace facade
2 changes: 2 additions & 0 deletions tests/dragonfly/connection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ async def client_pause():
await all


@pytest.mark.skip("The test deadlock")
async def test_tls_when_read_write_is_interleaved(
with_ca_tls_server_args, with_ca_tls_client_args, df_local_factory
):
Expand All @@ -772,6 +773,7 @@ async def test_tls_when_read_write_is_interleaved(
server: DflyInstance = df_local_factory.create(
port=1211, **with_ca_tls_server_args, proactor_threads=1
)
# TODO(kostas): to fix the deadlock in the test
server.start()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down

0 comments on commit b02521c

Please sign in to comment.