|
| 1 | +/* |
| 2 | +Copyright 2022 The Photon Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Verify ~ExecutorImpl won't self-join (EDEADLK → terminate) when |
| 18 | +// destroyed from its own worker thread. Related: #967, #969. |
| 19 | + |
| 20 | +#include <photon/common/executor/executor.h> |
| 21 | +#include <photon/common/alog.h> |
| 22 | +#include <photon/photon.h> |
| 23 | +#include "../../../test/gtest.h" |
| 24 | + |
| 25 | +#include <memory> |
| 26 | +#include <atomic> |
| 27 | +#include <future> |
| 28 | + |
| 29 | +using namespace photon; |
| 30 | + |
| 31 | +// Task releases the last shared_ptr ref on the worker thread, |
| 32 | +// triggering ~ExecutorImpl from within → must detach, not join. |
| 33 | +TEST(executor, destroy_from_worker_thread) { |
| 34 | + std::promise<void> done; |
| 35 | + auto fut = done.get_future(); |
| 36 | + |
| 37 | + { |
| 38 | + auto exec = std::make_shared<Executor>( |
| 39 | + photon::INIT_EVENT_DEFAULT, photon::INIT_IO_NONE); |
| 40 | + |
| 41 | + exec->async_perform(new auto([&exec, &done] { |
| 42 | + exec.reset(); // ~Executor on worker thread |
| 43 | + done.set_value(); // reachable only if no crash |
| 44 | + })); |
| 45 | + } |
| 46 | + |
| 47 | + fut.wait(); |
| 48 | + LOG_INFO("executor destroyed from worker thread without crash"); |
| 49 | +} |
0 commit comments