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


##########
test/brpc_server_tls_task_unittest.cpp:
##########
@@ -0,0 +1,668 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <errno.h>
+#include <gflags/gflags.h>
+#include <gtest/gtest.h>
+#include <vector>
+
+#include "bthread/bthread.h"
+#include "bthread/countdown_event.h"
+#include "bthread/unstable.h"
+#include "butil/atomicops.h"
+#include "butil/memory/scope_guard.h"
+#include "butil/time.h"
+#include "brpc/details/server_tls_context.h"
+#include "brpc/details/server_tls_task_binding.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace {
+using brpc::details::ServerTLSContext;
+using brpc::details::RegisterServerTLSTaskPool;
+using brpc::details::RetireServerTLSTaskPool;
+using Context = ServerTLSContext;
+
+struct BlockingTask {
+    Context* context = nullptr;
+    bthread_key_t key = INVALID_BTHREAD_KEY;
+    bool use_tls = false;
+    bool exit_explicitly = false;
+    bool function_released = false;
+    bool destructor_released = false;
+    butil::atomic<int> destroyed{0};
+    bthread::CountdownEvent started;
+    bthread::CountdownEvent finish_function;
+    bthread::CountdownEvent destructor_started;
+    bthread::CountdownEvent finish_destructor;
+
+    void ReleaseFunction() {
+        if (!function_released) {
+            function_released = true;
+            finish_function.signal();
+        }
+    }
+    void ReleaseDestructor() {
+        if (!destructor_released) {
+            destructor_released = true;
+            finish_destructor.signal();
+        }
+    }
+    static void Destroy(void* arg) {
+        auto* self = static_cast<BlockingTask*>(arg);
+        EXPECT_EQ(Context::Phase::DRAINING, 
self->context->GetSnapshot().phase);
+        EXPECT_GE(self->context->GetSnapshot().lease_count, 1u);
+        self->destructor_started.signal();
+        EXPECT_EQ(0, self->finish_destructor.timed_wait(
+            butil::seconds_from_now(30)));
+        self->destroyed.fetch_add(1, butil::memory_order_relaxed);
+    }
+    static void* Run(void* arg) {
+        auto* self = static_cast<BlockingTask*>(arg);
+        // The lease must already exist when the task executes, not only at 
exit.
+        EXPECT_GE(self->context->GetSnapshot().lease_count, 1u);
+        if (self->use_tls) {
+            EXPECT_EQ(0, bthread_setspecific(self->key, self));
+        }
+        self->started.signal();
+        EXPECT_EQ(0, self->finish_function.timed_wait(
+            butil::seconds_from_now(30)));
+        if (self->exit_explicitly) {
+            bthread_exit(nullptr);
+        }
+        return nullptr;
+    }
+};
+
+class ManagedPoolTest : public testing::Test {
+protected:
+    void SetUp() override {
+        context = new Context;
+        // Managed pool identities are retained even after retirement. Do not
+        // use a stack pool or recycle this address for another generation.
+        pool = new bthread_keytable_pool_t;
+        ASSERT_EQ(0, bthread_keytable_pool_init(pool));
+        initialized = true;
+        ASSERT_EQ(0, bthread_key_create(&key, BlockingTask::Destroy));
+        ASSERT_EQ(0, RegisterServerTLSTaskPool(pool, context));
+        registered = true;
+        ASSERT_EQ(0, context->CommitStartup());
+    }
+    void TearDown() override {
+        context->BeginDrain();
+        const timespec deadline = butil::seconds_from_now(5);
+        ASSERT_EQ(0, context->WaitSealed(&deadline));
+        if (initialized && !pool_destroyed) {
+            EXPECT_EQ(0, bthread_keytable_pool_destroy(pool));
+        }
+        if (key != INVALID_BTHREAD_KEY) {
+            EXPECT_EQ(0, bthread_key_delete(key));
+        }
+        EXPECT_EQ(0, context->Retire());
+        if (registered) {
+            EXPECT_EQ(0, RetireServerTLSTaskPool(pool));
+        }
+        // Keep the header and its rwlock valid for stale copied attributes.
+    }
+    bthread_attr_t Attr(bool pthread_stack = false, int tag = 0) const {
+        bthread_attr_t attr = pthread_stack ? BTHREAD_ATTR_PTHREAD
+                                           : BTHREAD_ATTR_NORMAL;
+        attr.keytable_pool = pool;
+        attr.tag = tag;
+        return attr;
+    }
+    scoped_refptr<Context> context;
+    bthread_keytable_pool_t* pool = nullptr;
+    bthread_key_t key = INVALID_BTHREAD_KEY;
+    bool initialized = false;
+    bool pool_destroyed = false;
+    bool registered = false;
+};
+
+int Start(bool urgent, bthread_t* tid, const bthread_attr_t* attr,
+          void* (*fn)(void*), void* arg) {
+    return urgent ? bthread_start_urgent(tid, attr, fn, arg)
+                  : bthread_start_background(tid, attr, fn, arg);
+}
+
+struct Options {
+    int caller;  // 0: native pthread; 1: same-tag bthread; 2: cross-tag 
bthread.
+    bool urgent;
+    bool pthread_stack;
+};
+
+struct Launcher {
+    Launcher(Options options_in, bthread_attr_t attr_in, void* data_in,
+             void* (*fn_in)(void*) = BlockingTask::Run)
+        : options(options_in), attr(attr_in), data(data_in), fn(fn_in) {}
+    Options options;
+    bthread_attr_t attr;
+    void* data;
+    void* (*fn)(void*);
+    bthread_t child = INVALID_BTHREAD;
+    int result = -1;
+    bthread::CountdownEvent completed;
+    static void* Run(void* arg) {
+        auto* self = static_cast<Launcher*>(arg);
+        self->result = Start(self->options.urgent, &self->child, &self->attr,
+                             self->fn, self->data);
+        self->completed.signal();
+        return nullptr;
+    }
+};
+
+class ManagedTaskTest : public ManagedPoolTest,
+                        public testing::WithParamInterface<Options> {
+protected:
+    void CheckExitBoundary(bool use_tls, bool exit_explicitly = false) {
+        const Options options = GetParam();
+        BlockingTask data;
+        data.context = context.get();
+        data.key = key;
+        data.use_tls = use_tls;
+        data.exit_explicitly = exit_explicitly;
+        Launcher launcher{options,
+                          Attr(options.pthread_stack, options.caller == 2 ? 1 
: 0),
+                          &data};
+        bthread_t driver = INVALID_BTHREAD;
+        auto cleanup = butil::MakeScopeGuard([&] {
+            data.ReleaseFunction();
+            data.ReleaseDestructor();
+            // Join driver before reading its output even on a fatal assertion.
+            if (driver != INVALID_BTHREAD) {
+                EXPECT_EQ(0, bthread_join(driver, nullptr));
+            }
+            if (launcher.child != INVALID_BTHREAD) {
+                EXPECT_EQ(0, bthread_join(launcher.child, nullptr));
+            }
+        });

Review Comment:
   When `use_tls=true` and the preceding `data.started.timed_wait()` times out, 
this guard unblocks and joins the child task, but the pool has not yet been 
destroyed. `return_keytable()` then caches a KeyTable containing `&data`. 
`CheckExitBoundary()` subsequently returns and the local `data` is destroyed. 
`BlockingTask::Destroy()` is not called until the fixture's `TearDown()` 
destroys the pool, at which point the callback accesses an invalid stack 
object. Please ensure that scope cleanup destroys the pool's TLS values before 
`data` goes out of scope, and update `pool_destroyed`.



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