Copilot commented on code in PR #3708:
URL: https://github.com/apache/brpc/pull/3708#discussion_r4677804844


##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -908,26 +979,136 @@ int UrmaEndpoint::SendImm(uint32_t imm) {
                      << _resource->remote_jetty->id.id
                      << " state=" << GetStateStr()
                      << " on " << _socket->description();
+        ReportFailure(UrmaFailureReason::SEND_POST);
         errno = status;
         return -1;
     }
     return 0;
 }
 
 int UrmaEndpoint::SendAck(int num) {
-    const uint16_t old =
-        _new_rq_wrs.fetch_add(num, butil::memory_order_relaxed);
-    if (old + num > _remote_window_capacity / 2 &&
-        _sq_imm_window_size > 0) {
-        return SendImm(_new_rq_wrs.exchange(0, butil::memory_order_relaxed));
+    const uint16_t pending =
+        _new_rq_wrs.fetch_add(num, butil::memory_order_relaxed) + num;
+    if (pending == 0 || _sq_imm_window_size == 0) {
+        return 0;
+    }
+    if (pending > _remote_window_capacity / 2) {
+        return FlushAck();
     }
+    // The peer's SQ may run out before we reach our RQ-based threshold.
+    return ScheduleAckFlush();
+}
+
+int UrmaEndpoint::FlushAck() {
+    if (_sq_imm_window_size == 0) {
+        return 0;
+    }
+    if (SendImm(_new_rq_wrs.exchange(0, butil::memory_order_relaxed)) == 0) {
+        return 0;
+    }
+    return errno == EAGAIN ? ScheduleAckFlush() : -1;
+}
+
+int UrmaEndpoint::ScheduleAckFlush() {
+    if (_ack_flush_scheduled) {
+        return 0;
+    }
+    bthread_t tid;
+    bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
+    attr.tag = bthread_self_tag();
+    const int rc = bthread_start_background(
+        &tid, &attr, RunAckFlush, reinterpret_cast<void*>(_cq_sid));
+    if (rc != 0) {
+        errno = rc;
+        return -1;
+    }
+    _ack_flush_scheduled = true;

Review Comment:
   `RunAckFlush` can finish before this assignment because 
`bthread_start_background` publishes the worker immediately. If the worker 
wakes after the 1 ms delay while `_ack_flush_scheduled` is still false, it 
clears the flag at line 1045 and then this caller sets it back to true; 
subsequent `SendAck` calls will believe a flush is permanently scheduled and no 
longer retry, leaving receive credits queued until the connection stalls. Mark 
the task scheduled before starting it and clear the flag if thread creation 
fails.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to