chenBright commented on code in PR #3647:
URL: https://github.com/apache/brpc/pull/3647#discussion_r4358021709


##########
src/brpc/span.cpp:
##########
@@ -847,22 +874,31 @@ leveldb::Status SpanDB::RemoveSpansBefore(int64_t tm) {
 
 // Write span into leveldb.
 void Span::dump_to_db() {
+    // This must outlive all locals, including the local DB reference: shutdown
+    // must wait for both persistence and any resulting DB destruction.
+    BAIDU_SCOPED_LOCK(g_span_dump_mutex);
+    {
+        BAIDU_SCOPED_LOCK(g_span_db_mutex);
+        if (g_span_ending) {
+            return;
+        }
+    }
     StartIndexingIfNeeded();
 
     std::string value_buf;
 
     butil::intrusive_ptr<SpanDB> db;
     if (GetSpanDB(&db) != 0) {
-        if (g_span_ending) {
+        // Own the DB before publishing it. If shutdown started during Open(),
+        // the rejected DB is released before we leave the dump mutex.
+        db.reset(SpanDB::Open());
+        if (!db) {
+            LOG(WARNING) << "Fail to open SpanDB";
             return;
         }
-        SpanDB* db2 = SpanDB::Open();
-        if (db2 == nullptr) {
-            LOG(WARNING) << "Fail to open SpanDB";
+        if (!ResetSpanDB(db.get())) {
             return;
         }
-        ResetSpanDB(db2);
-        db.reset(db2);
     }
 
     leveldb::Status st = db->Index(shared_from_this(), &value_buf);

Review Comment:
   Same as above.



-- 
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