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


##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,388 @@
+/** @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.
+ */
+
+#define CATCH_CONFIG_THREAD_SAFE_ASSERTIONS
+#include "inkevent_test_fixtures.h"
+
+#include <tscore/ink_assert.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);
+    this->callback_action = eventProcessor.schedule_imm(this, ET_CALL);
+    ink_assert(this->callback_action != nullptr);
+  }
+
+  // In case of an exception in a thread that would have set release, we set
+  // it here in order to unfreeze any threads that may be waiting on done.
+  ~HoldOnEThread()
+  {
+    this->release.set();
+    this->cancel_callback();
+    this->done.wait_until_set();
+  }

Review Comment:
   This can deadlock the test process: `~HoldOnEThread()` waits unconditionally 
on `done`, but `on_event()` only sets `done` when `release.wait_until_set()` 
returns true. Your own comment notes that `wait_until_set()` can time out, 
which implies an execution path where `on_event()` returns without setting 
`done` and `cancel_callback()` won’t set it either (because `held` is already 
set), leaving the destructor blocked forever. Make `done` get set on all exit 
paths of `on_event()` (e.g., set it after the `if` regardless of wait result), 
or change the destructor to avoid an unbounded wait (e.g., timed wait + fail 
the test / set `done` defensively).



##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,388 @@
+/** @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.
+ */
+
+#define CATCH_CONFIG_THREAD_SAFE_ASSERTIONS
+#include "inkevent_test_fixtures.h"
+
+#include <tscore/ink_assert.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);
+    this->callback_action = eventProcessor.schedule_imm(this, ET_CALL);
+    ink_assert(this->callback_action != nullptr);
+  }
+
+  // In case of an exception in a thread that would have set release, we set
+  // it here in order to unfreeze any threads that may be waiting on done.
+  ~HoldOnEThread()
+  {
+    this->release.set();
+    this->cancel_callback();
+    this->done.wait_until_set();
+  }
+
+  bool
+  wait_for_callback_start()
+  {
+    return this->held.wait_until_set();
+  }
+
+  bool
+  wait_for_callback_finish()
+  {
+    this->release.set();
+    // The callback can finish without setting done due to wait timeouts. We
+    // return false in that case.
+    return this->done.wait_until_set();
+  }
+
+private:
+  Action         *callback_action{};
+  Ptr<ProxyMutex> target_mutex;
+  AtomicFlag      held;
+  AtomicFlag      release;
+  AtomicFlag      done;
+
+  int
+  on_event(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */)
+  {
+    SCOPED_MUTEX_LOCK(guard, this->target_mutex, this_ethread());
+    this->held.set();
+    if (this->release.wait_until_set()) {
+      MUTEX_RELEASE(guard);
+      this->done.set();
+    }
+    return 0;

Review Comment:
   This can deadlock the test process: `~HoldOnEThread()` waits unconditionally 
on `done`, but `on_event()` only sets `done` when `release.wait_until_set()` 
returns true. Your own comment notes that `wait_until_set()` can time out, 
which implies an execution path where `on_event()` returns without setting 
`done` and `cancel_callback()` won’t set it either (because `held` is already 
set), leaving the destructor blocked forever. Make `done` get set on all exit 
paths of `on_event()` (e.g., set it after the `if` regardless of wait result), 
or change the destructor to avoid an unbounded wait (e.g., timed wait + fail 
the test / set `done` defensively).



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