Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3683899159


##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,351 @@
+/** @file
+
+  Catch2 unit tests for the Lock.h mutex acquisition macros.
+
+  @section license License
+
+  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 "inkevent_test_fixtures.h"
+
+using inkevent_test::AtomicFlag;
+using inkevent_test::EventProcessorListener;
+
+CATCH_REGISTER_LISTENER(EventProcessorListener)
+
+namespace
+{
+
+class HoldOnEThread : public Continuation
+{
+public:
+  HoldOnEThread(ProxyMutex *self_mutex, Ptr<ProxyMutex> &target) : 
Continuation(self_mutex), target_mutex(target)
+  {
+    SET_HANDLER(&HoldOnEThread::on_event);
+  }
+
+  Ptr<ProxyMutex> target_mutex;
+  AtomicFlag      held;
+  AtomicFlag      release;
+  AtomicFlag      done;
+
+private:
+  int
+  on_event(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */)
+  {
+    {
+      SCOPED_MUTEX_LOCK(guard, target_mutex, this_ethread());
+      held.set();
+      release.wait_until_set();
+    }
+    done.set();
+    return 0;
+  }
+};
+
+} // namespace
+
+TEST_CASE("MUTEX_TRY_LOCK on an unheld ProxyMutex constructs a guard whose 
is_locked() reports the successful acquisition",
+          "[inkevent][lock]")
+{
+  Ptr<ProxyMutex> m{new_ProxyMutex()};
+  EThread        *t = this_ethread();
+
+  MUTEX_TRY_LOCK(guard, m, t);
+
+  REQUIRE(guard.is_locked());
+  REQUIRE(m->thread_holding == t);
+  REQUIRE(m->nthread_holding == 1);
+}
+
+TEST_CASE("MUTEX_TRY_LOCK against a contended ProxyMutex constructs a guard 
whose is_locked() reports the failed acquisition",
+          "[inkevent][lock][multithread]")
+{
+  Ptr<ProxyMutex> contended{new_ProxyMutex()};
+  Ptr<ProxyMutex> cont_self{new_ProxyMutex()};
+  HoldOnEThread   holder{cont_self.get(), contended};
+
+  REQUIRE(eventProcessor.schedule_imm(&holder, ET_CALL) != nullptr);
+  REQUIRE(holder.held.wait_until_set());
+
+  EThread *t = this_ethread();
+  MUTEX_TRY_LOCK(guard, contended, t);
+
+  REQUIRE_FALSE(guard.is_locked());
+
+  holder.release.set();
+  REQUIRE(holder.done.wait_until_set());
+}

Review Comment:
   The `HoldOnEThread holder` continuation is stack-allocated but scheduled 
asynchronously. If any `REQUIRE(...)` throws before `holder.release.set()` / 
`holder.done.wait_until_set()` runs, the scheduled EThread can block forever on 
`release.wait_until_set()` and/or continue executing after `holder` has been 
destroyed during stack unwinding (use-after-free). To make the test 
exception-safe, add an unconditional cleanup mechanism immediately after 
scheduling (e.g., a scope-exit guard) that always signals `release` and waits 
for `done`, or allocate the continuation with a lifetime that extends until 
completion and ensure it self-owns / is joined before scope exit. Same pattern 
appears in the other `[multithread]` test cases using `HoldOnEThread`.



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

Reply via email to