From 0bcb033349145bf147735a8ff36b0fc09360bf92 Mon Sep 17 00:00:00 2001
From: Cory McWilliams <cory@unprompted.com>
Date: Wed, 19 Mar 2025 06:46:34 -0400
Subject: [PATCH] ssb: This seems like it would explain replication stalls.

---
 src/ssb.c     | 2 +-
 src/ssb.rpc.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/ssb.c b/src/ssb.c
index 15f3ce43d..5830ee77c 100644
--- a/src/ssb.c
+++ b/src/ssb.c
@@ -4580,7 +4580,7 @@ void tf_ssb_connection_adjust_read_backpressure(tf_ssb_connection_t* connection,
 void tf_ssb_connection_adjust_write_count(tf_ssb_connection_t* connection, int delta)
 {
 	connection->active_write_count += delta;
-	if (!connection->is_closing)
+	if (!connection->is_closing && connection->active_write_count == 0)
 	{
 		uv_async_send(&connection->scheduled_async);
 	}
diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c
index 074806f43..30f1a91ea 100644
--- a/src/ssb.rpc.c
+++ b/src/ssb.rpc.c
@@ -984,9 +984,9 @@ static void _tf_ssb_connection_send_history_stream_after_work(tf_ssb_connection_
 
 static void _tf_ssb_connection_send_history_stream_callback(tf_ssb_connection_t* connection, bool skip, void* user_data)
 {
-	tf_ssb_connection_adjust_write_count(connection, 1);
 	if (!skip && tf_ssb_connection_is_connected(connection) && !tf_ssb_is_shutting_down(tf_ssb_connection_get_ssb(connection)))
 	{
+		tf_ssb_connection_adjust_write_count(connection, 1);
 		tf_ssb_connection_run_work(connection, _tf_ssb_connection_send_history_stream_work, _tf_ssb_connection_send_history_stream_after_work, user_data);
 	}
 	else