You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "JosiahWI (via GitHub)" <gi...@apache.org> on 2026/07/28 11:34:58 UTC
[PR] Add unit tests for lock macros [trafficserver]
JosiahWI opened a new pull request, #13439:
URL: https://github.com/apache/trafficserver/pull/13439
These tests cover the behavior of the event system macros for locking and unlocking `ProtyMutex` objects.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3665246308
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,357 @@
+/** @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]")
Review Comment:
The PR description mentions locking/unlocking `ProtyMutex`, but the new tests (and the codebase type) use `ProxyMutex`. Please update the PR description to avoid confusion about what is being tested.
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,357 @@
+/** @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);
+ }
Review Comment:
`target` is not modified, so this should be a `const Ptr<ProxyMutex>&` (or passed by value) rather than a non-const reference. Using a non-const reference is unnecessarily restrictive (can’t bind temporaries) and suggests mutation that doesn’t occur.
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,357 @@
+/** @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());
+}
+
+TEST_CASE("MUTEX_TRY_LOCK by the holding thread is reentrant and returns a guard reporting is_locked() == true", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(outer, m, t);
+ REQUIRE(outer.is_locked());
+ REQUIRE(m->nthread_holding == 1);
+
+ {
+ MUTEX_TRY_LOCK(inner, m, t);
+ REQUIRE(inner.is_locked());
+ REQUIRE(m->nthread_holding == 2);
+ }
+
+ REQUIRE(m->nthread_holding == 1);
+ REQUIRE(m->thread_holding == t);
+}
+
+TEST_CASE("A MUTEX_TRY_LOCK guard releases the lock at scope exit, leaving the ProxyMutex unheld", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ MUTEX_TRY_LOCK(guard, m, t);
+ REQUIRE(guard.is_locked());
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_RELEASE on a MUTEX_TRY_LOCK guard releases the lock early and flips is_locked() to false", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(guard, m, t);
+ REQUIRE(guard.is_locked());
+
+ MUTEX_RELEASE(guard);
+
+ REQUIRE_FALSE(guard.is_locked());
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_RELEASE invoked twice on the same MUTEX_TRY_LOCK guard is a no-op on the second call", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(guard, m, t);
+ MUTEX_RELEASE(guard);
+ REQUIRE_FALSE(guard.is_locked());
+
+ MUTEX_RELEASE(guard);
+
+ REQUIRE_FALSE(guard.is_locked());
+ REQUIRE(m->nthread_holding == 0);
+}
+
+TEST_CASE("SCOPED_MUTEX_LOCK on an unheld ProxyMutex acquires the lock during construction", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ SCOPED_MUTEX_LOCK(guard, m, t);
+
+ REQUIRE(m->thread_holding == t);
+ REQUIRE(m->nthread_holding == 1);
+}
+
+TEST_CASE("A SCOPED_MUTEX_LOCK guard releases the lock when its enclosing scope ends", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ SCOPED_MUTEX_LOCK(guard, m, t);
+ REQUIRE(m->nthread_holding == 1);
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("SCOPED_MUTEX_LOCK is reentrant when the calling EThread already holds the lock", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ SCOPED_MUTEX_LOCK(outer, m, t);
+ REQUIRE(m->nthread_holding == 1);
+
+ {
+ SCOPED_MUTEX_LOCK(inner, m, t);
+ REQUIRE(m->nthread_holding == 2);
+ REQUIRE(m->thread_holding == t);
+ }
+
+ REQUIRE(m->nthread_holding == 1);
+ REQUIRE(m->thread_holding == t);
+}
+
+TEST_CASE("MUTEX_RELEASE on a SCOPED_MUTEX_LOCK guard releases the lock early and renders the destructor a no-op",
+ "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ SCOPED_MUTEX_LOCK(guard, m, t);
+ MUTEX_RELEASE(guard);
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_TAKE_LOCK acquires an unheld ProxyMutex and records the calling thread as its holder", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TAKE_LOCK(m, t);
+
+ REQUIRE(m->thread_holding == t);
+ REQUIRE(m->nthread_holding == 1);
+
+ MUTEX_UNTAKE_LOCK(m, t);
+}
+
+TEST_CASE("MUTEX_TAKE_LOCK by the holding thread is reentrant and increments the reentry count without blocking",
+ "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TAKE_LOCK(m, t);
+ MUTEX_TAKE_LOCK(m, t);
+
+ REQUIRE(m->nthread_holding == 2);
+ REQUIRE(m->thread_holding == t);
+
+ MUTEX_UNTAKE_LOCK(m, t);
+ MUTEX_UNTAKE_LOCK(m, t);
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("After the holding EThread fully releases a contended ProxyMutex, MUTEX_TRY_LOCK on the main thread succeeds",
+ "[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());
+
+ holder.release.set();
+ REQUIRE(holder.done.wait_until_set());
+
+ EThread *t = this_ethread();
+ MUTEX_TRY_LOCK(guard, contended, t);
+
+ REQUIRE(guard.is_locked());
+ REQUIRE(contended->thread_holding == t);
+}
+
+TEST_CASE("WEAK_SCOPED_MUTEX_LOCK on a non-null ProxyMutex acquires the lock during construction", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ WEAK_SCOPED_MUTEX_LOCK(guard, m, t);
+
+ REQUIRE(m->thread_holding == t);
+ REQUIRE(m->nthread_holding == 1);
+}
+
+TEST_CASE("A WEAK_SCOPED_MUTEX_LOCK guard releases its lock when its enclosing scope ends", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ WEAK_SCOPED_MUTEX_LOCK(guard, m, t);
+ REQUIRE(m->nthread_holding == 1);
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("WEAK_SCOPED_MUTEX_LOCK on a null Ptr<ProxyMutex> is a no-op that takes and releases no lock", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> empty;
+ Ptr<ProxyMutex> witness{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ REQUIRE(empty.get() == nullptr);
+
+ {
+ WEAK_SCOPED_MUTEX_LOCK(guard, empty, t);
+ REQUIRE(witness->thread_holding == nullptr);
+ REQUIRE(witness->nthread_holding == 0);
+ }
+
+ REQUIRE(witness->thread_holding == nullptr);
+ REQUIRE(witness->nthread_holding == 0);
+}
Review Comment:
This test largely asserts default invariants on `witness` (which is never involved in the operation), so it will pass even if `WEAK_SCOPED_MUTEX_LOCK` is broken in ways that don’t crash. Consider asserting directly against observable behavior of `guard` (e.g., its locked state if available), or otherwise constructing an assertion that would fail if the macro attempted to lock/modify state when given a null mutex.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714424104
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,393 @@
+/** @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>
+
+#include <cstdint>
+
+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->cancel_callback();
+ this->wait_for_callback_finish();
+ }
+
+ 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;
+ }
+
+ void
+ cancel_callback()
+ {
+ SCOPED_MUTEX_LOCK(guard, this->mutex, this_ethread());
+ if (this->is_expecting_callback()) {
+ this->callback_action->cancel(this);
+ ink_assert(this->callback_action->cancelled);
+ this->done.set();
+ }
Review Comment:
Incorrect. Continuations call handlers back with the continuation mutex locked. The cancellation happens with the lock, so it can't possibly happen during the callback.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714502691
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,393 @@
+/** @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>
+
+#include <cstdint>
+
+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->cancel_callback();
+ this->wait_for_callback_finish();
+ }
+
+ 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();
+ }
Review Comment:
This helper explicitly documents that `wait_for_callback_finish()` can return false due to timeouts, but the multithreaded tests later use `REQUIRE(holder.wait_for_callback_finish())`, which makes the suite prone to intermittent CI failures if the timeout is hit. Prefer making completion deterministic (e.g., ensure the callback always sets `done` on all exit paths and/or use a non-timeout wait for this test synchronization), or change the tests to fail with a clearer message and a larger/explicit timeout that’s appropriate for CI.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#issuecomment-5179371798
@masaori335 I have cleaned up the synchronization. I ran your command and it passed. I've also run the test through ASan and TSan. Curiously, TSan detects a data race on `ProxyMutex::thread_holding` in debug builds (not in release builds). Sure enough, accesses to `ProxyMutex::thread_holding` in the locking code are unsynchronized.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3712792531
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,381 @@
+/** @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"
+
+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);
+ }
+
+ // 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 schedules `this` onto another thread, but `HoldOnEThread` is stack-allocated in the tests. The destructor attempts to prevent use-after-free by canceling the action and waiting on `done`, but `cancel_callback()` sets `done` immediately after calling `cancel()`, without establishing that the callback cannot still run (cancellation may be best-effort depending on timing). If the callback executes after `done` is set and the destructor returns, it could dereference a freed object. Prefer making the continuation lifetime explicit (e.g., heap-allocate and ensure the callback owns/releases it, or only signal `done` after you have a guaranteed \"won't run\" outcome from cancellation / a definitive synchronization that the callback has not started).
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,381 @@
+/** @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"
+
+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);
+ }
+
+ // 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();
+ 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:
`done` is only set when `release.wait_until_set()` returns true. Since this method returns `bool`, a `false` return path appears possible; if that happens, `~HoldOnEThread()` will block forever on `done.wait_until_set()`, hanging the test process. To make the test robust, ensure `done` is always signaled (e.g., set `done` unconditionally before returning, or handle the `false` path explicitly).
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3713904421
##########
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);
+ }
Review Comment:
`eventProcessor.schedule_imm()` may return a sentinel `Action*` (e.g. `ACTION_RESULT_DONE`) rather than a real pointer. This class later dereferences `callback_action` (e.g. `callback_action->cancelled`), which would be undefined behavior/crash for sentinel values (low bit set). Handle sentinel returns explicitly (check `(uintptr_t)callback_action & 1`): if a sentinel is returned, treat the callback as already completed (set the appropriate flags) and avoid storing/dereferencing it as an `Action*`.
##########
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:
The destructor can block forever. `on_event()` only sets `done` when `release.wait_until_set()` succeeds, and the comment explicitly notes the wait can time out; in that case `done` is never set. If the callback has already started (`held` set) and then timed out, `cancel_callback()` won’t cancel (because `is_expecting_callback()` returns false once `held` is set), leaving `done` unset and the destructor waiting indefinitely. Make `done` guaranteed to be set on all callback exit paths (including timeout), or change the destructor to avoid an unbounded wait (e.g., timed wait + fallback/cleanup).
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3691232323
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,357 @@
+/** @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);
+ }
Review Comment:
It's a one-line function and I don't want to change this unless a human asks.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3691304632
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,361 @@
+/** @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"
+
+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);
+ }
+
+ // 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()
+ {
+ release.set();
+ done.wait_until_set();
+ }
+
+ 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();
+ // Non-fatal so that done gets set.
+ CHECK(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());
+}
+
+TEST_CASE("MUTEX_TRY_LOCK by the holding thread is reentrant and returns a guard reporting is_locked() == true", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(outer, m, t);
+ REQUIRE(outer.is_locked());
+ REQUIRE(m->nthread_holding == 1);
+
+ {
+ MUTEX_TRY_LOCK(inner, m, t);
+ REQUIRE(inner.is_locked());
+ REQUIRE(m->nthread_holding == 2);
+ }
+
+ REQUIRE(m->nthread_holding == 1);
+ REQUIRE(m->thread_holding == t);
+}
+
+TEST_CASE("A MUTEX_TRY_LOCK guard releases the lock at scope exit, leaving the ProxyMutex unheld", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ MUTEX_TRY_LOCK(guard, m, t);
+ REQUIRE(guard.is_locked());
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_RELEASE on a MUTEX_TRY_LOCK guard releases the lock early and flips is_locked() to false", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(guard, m, t);
+ REQUIRE(guard.is_locked());
+
+ MUTEX_RELEASE(guard);
+
+ REQUIRE_FALSE(guard.is_locked());
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_RELEASE invoked twice on the same MUTEX_TRY_LOCK guard is a no-op on the second call", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TRY_LOCK(guard, m, t);
+ MUTEX_RELEASE(guard);
+ REQUIRE_FALSE(guard.is_locked());
+
+ MUTEX_RELEASE(guard);
+
+ REQUIRE_FALSE(guard.is_locked());
+ REQUIRE(m->nthread_holding == 0);
+}
+
+TEST_CASE("SCOPED_MUTEX_LOCK on an unheld ProxyMutex acquires the lock during construction", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ SCOPED_MUTEX_LOCK(guard, m, t);
+
+ REQUIRE(m->thread_holding == t);
+ REQUIRE(m->nthread_holding == 1);
+}
+
+TEST_CASE("A SCOPED_MUTEX_LOCK guard releases the lock when its enclosing scope ends", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ SCOPED_MUTEX_LOCK(guard, m, t);
+ REQUIRE(m->nthread_holding == 1);
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("SCOPED_MUTEX_LOCK is reentrant when the calling EThread already holds the lock", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ SCOPED_MUTEX_LOCK(outer, m, t);
+ REQUIRE(m->nthread_holding == 1);
+
+ {
+ SCOPED_MUTEX_LOCK(inner, m, t);
+ REQUIRE(m->nthread_holding == 2);
+ REQUIRE(m->thread_holding == t);
+ }
+
+ REQUIRE(m->nthread_holding == 1);
+ REQUIRE(m->thread_holding == t);
+}
+
+TEST_CASE("MUTEX_RELEASE on a SCOPED_MUTEX_LOCK guard releases the lock early and renders the destructor a no-op",
+ "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ {
+ SCOPED_MUTEX_LOCK(guard, m, t);
+ MUTEX_RELEASE(guard);
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+ }
+
+ REQUIRE(m->nthread_holding == 0);
+ REQUIRE(m->thread_holding == nullptr);
+}
+
+TEST_CASE("MUTEX_TAKE_LOCK acquires an unheld ProxyMutex and records the calling thread as its holder", "[inkevent][lock]")
+{
+ Ptr<ProxyMutex> m{new_ProxyMutex()};
+ EThread *t = this_ethread();
+
+ MUTEX_TAKE_LOCK(m, t);
+
+ REQUIRE(m->thread_holding == t);
+ REQUIRE(m->nthread_holding == 1);
+
+ MUTEX_UNTAKE_LOCK(m, t);
+}
Review Comment:
The test acquires a ProxyMutex via MUTEX_TAKE_LOCK but only unlocks it at the end of the test case. If any REQUIRE below the lock acquisition fails, stack unwinding will skip MUTEX_UNTAKE_LOCK and the ProxyMutex can be destroyed while its underlying ink_mutex is still locked (ProxyMutex::free() unconditionally calls ink_mutex_destroy), which is undefined behavior and can crash/hang the test runner. Add an RAII cleanup so the mutex is always released even when assertions throw.
This issue also appears on line 248 of the same file.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#issuecomment-5174679452
This looks flaky. If I run this test repeatedly like below, it fails. Is it expected?
```
ctest --test-dir build-asf-master-review-0-debug -R '^test_Lock$' --repeat until-fail:100 --output-on-failure
```
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714136674
##########
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:
Incorrect. This wait can also time out.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714140812
##########
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:
How many times do I have to say this is incorrect?
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI merged PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714395692
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,393 @@
+/** @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>
+
+#include <cstdint>
+
+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->cancel_callback();
+ this->wait_for_callback_finish();
+ }
+
+ 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;
+ }
+
+ void
+ cancel_callback()
+ {
+ SCOPED_MUTEX_LOCK(guard, this->mutex, this_ethread());
+ if (this->is_expecting_callback()) {
+ this->callback_action->cancel(this);
+ ink_assert(this->callback_action->cancelled);
+ this->done.set();
+ }
Review Comment:
`ink_assert(this->callback_action->cancelled);` is racy and can make the tests flaky/crash: the scheduled callback may already be executing (or about to execute) when the destructor runs, so `cancel()` may not transition `cancelled` to true even though the callback is in-flight. Suggestion (mandatory): remove the hard assert and handle both outcomes (canceled vs already-running) by unblocking the callback via `release.set()` and/or by waiting for a completion signal without assuming cancellation succeeded.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3714896070
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,393 @@
+/** @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>
+
+#include <cstdint>
+
+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->cancel_callback();
+ this->wait_for_callback_finish();
+ }
+
+ 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();
+ }
Review Comment:
I think the timeout is plenty that we should be OK in this case, and if the timeout does trigger, it might indicate performance problems.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#issuecomment-5182637621
[approve ci debian]
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3713717651
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,381 @@
+/** @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"
+
+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);
+ }
+
+ // 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();
+ 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 analysis is incorrect. The `done` flag is intentionally not set on the negative branch, so that `done.wait_until_set` will time out and return `false`, indicating a failure.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3683259345
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,349 @@
+/** @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;
Review Comment:
`done` is signaled while the `SCOPED_MUTEX_LOCK` guard is still in scope, meaning the mutex may still be held when other threads observe `done`. This can make the multithreaded tests flaky (e.g., a `MUTEX_TRY_LOCK` immediately after `done` may still fail). Move `done.set()` to after the guard is destroyed (e.g., wrap the lock section in an inner scope and signal `done` after that scope), or explicitly release the guard before setting `done`.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#issuecomment-5161204620
[approve ci]
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#issuecomment-5181953645
The parent selection regression test failed on Debian.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add unit tests for lock macros [trafficserver]
Posted by "JosiahWI (via GitHub)" <gi...@apache.org>.
JosiahWI commented on code in PR #13439:
URL: https://github.com/apache/trafficserver/pull/13439#discussion_r3713725585
##########
src/iocore/eventsystem/unit_tests/test_Lock.cc:
##########
@@ -0,0 +1,381 @@
+/** @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"
+
+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);
+ }
+
+ // 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 analysis is incorrect. The cancellation cannot fail.
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org