Skip to content

Commit 2f9bb06

Browse files
committed
Fix crash.
1 parent 4a07e27 commit 2f9bb06

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

Common/Cpp/Concurrency/AsyncTask.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ AsyncTask::~AsyncTask(){
3838

3939

4040
void AsyncTask::report_cancelled() noexcept{
41+
#ifdef PA_SANITIZE_AsyncTask
42+
auto scope = m_sanitizer.check_scope();
43+
#endif
4144
m_state.store(State::FINISHED, std::memory_order_release);
4245
{
4346
std::lock_guard<std::mutex> lg(m_lock);
@@ -46,6 +49,9 @@ void AsyncTask::report_cancelled() noexcept{
4649
m_state.store(State::SAFE_TO_DESTRUCT, std::memory_order_release);
4750
}
4851
void AsyncTask::run() noexcept{
52+
#ifdef PA_SANITIZE_AsyncTask
53+
auto scope = m_sanitizer.check_scope();
54+
#endif
4955
try{
5056
m_task();
5157
}catch (...){

Common/Cpp/Concurrency/AsyncTask.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include <mutex>
1414
#include <condition_variable>
1515

16+
#define PA_SANITIZE_AsyncTask
17+
18+
#ifdef PA_SANITIZE_AsyncTask
19+
#include "Common/Cpp/LifetimeSanitizer.h"
20+
#endif
21+
1622
namespace PokemonAutomation{
1723

1824

@@ -40,12 +46,18 @@ class AsyncTask{
4046
{}
4147

4248
bool is_finished() const noexcept{
49+
#ifdef PA_SANITIZE_AsyncTask
50+
auto scope = m_sanitizer.check_scope();
51+
#endif
4352
State state = m_state.load(std::memory_order_acquire);
4453
return state == State::FINISHED || state == State::SAFE_TO_DESTRUCT;
4554
}
4655

4756
// Wait for the task to finish. Will rethrow any exceptions.
4857
void wait_and_rethrow_exceptions(){
58+
#ifdef PA_SANITIZE_AsyncTask
59+
auto scope = m_sanitizer.check_scope();
60+
#endif
4961
if (!is_finished()){
5062
std::unique_lock<std::mutex> lg(m_lock);
5163
m_cv.wait(lg, [this]{ return is_finished(); });
@@ -60,6 +72,9 @@ class AsyncTask{
6072
// These should only be called inside a parallel framework.
6173
// These are not thread-safe with each other.
6274
void report_started(){
75+
#ifdef PA_SANITIZE_AsyncTask
76+
auto scope = m_sanitizer.check_scope();
77+
#endif
6378
m_state.store(State::RUNNING, std::memory_order_release);
6479
}
6580
void report_cancelled() noexcept;
@@ -72,6 +87,10 @@ class AsyncTask{
7287
std::exception_ptr m_exception;
7388
mutable std::mutex m_lock;
7489
std::condition_variable m_cv;
90+
91+
#ifdef PA_SANITIZE_AsyncTask
92+
LifetimeSanitizer m_sanitizer;
93+
#endif
7594
};
7695

7796

Common/Cpp/LifetimeSanitizer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ void LifetimeSanitizer::check_usage() const{
105105
return;
106106
}
107107
ReadSpinLock lg(sanitizer_lock);
108-
if (SANITIZER_FILTER.contains(m_name)){
109-
std::cout << "LifetimeSanitizer - Using: " << this << " : " << m_name << std::endl;
110-
}
111108
auto iter = sanitizer_map.find(this);
112109
if (iter == sanitizer_map.end()){
113-
std::cerr << "Use non-existent: " << this << " : " << m_name << std::endl;
110+
std::cerr << "Use non-existent: " << this << std::endl;
114111
terminate_with_dump();
115112
}
113+
if (SANITIZER_FILTER.contains(m_name)){
114+
std::cout << "LifetimeSanitizer - Using: " << this << " : " << m_name << std::endl;
115+
}
116116
if (m_token != SANITIZER_TOKEN || m_self != this){
117117
std::cerr << "Use corrupted: " << this << " : " << m_name << std::endl;
118118
terminate_with_dump();
@@ -123,14 +123,14 @@ void LifetimeSanitizer::start_using() const{
123123
return;
124124
}
125125
ReadSpinLock lg(sanitizer_lock);
126-
if (SANITIZER_FILTER.contains(m_name)){
127-
std::cout << "LifetimeSanitizer - Start using: " << this << " : " << m_name << std::endl;
128-
}
129126
auto iter = sanitizer_map.find(this);
130127
if (iter == sanitizer_map.end()){
131-
std::cerr << "Start using non-existent: " << this << " : " << m_name << std::endl;
128+
std::cerr << "Start using non-existent: " << this << std::endl;
132129
terminate_with_dump();
133130
}
131+
if (SANITIZER_FILTER.contains(m_name)){
132+
std::cout << "LifetimeSanitizer - Start using: " << this << " : " << m_name << std::endl;
133+
}
134134
if (m_token != SANITIZER_TOKEN || m_self != this){
135135
std::cerr << "Start using corrupted: " << this << " : " << m_name << std::endl;
136136
terminate_with_dump();
@@ -142,14 +142,14 @@ void LifetimeSanitizer::done_using() const{
142142
return;
143143
}
144144
ReadSpinLock lg(sanitizer_lock);
145-
if (SANITIZER_FILTER.contains(m_name)){
146-
std::cout << "LifetimeSanitizer - Done using: " << this << " : " << m_name << std::endl;
147-
}
148145
auto iter = sanitizer_map.find(this);
149146
if (iter == sanitizer_map.end()){
150-
std::cerr << "Done using non-existent: " << this << " : " << m_name << std::endl;
147+
std::cerr << "Done using non-existent: " << this << std::endl;
151148
terminate_with_dump();
152149
}
150+
if (SANITIZER_FILTER.contains(m_name)){
151+
std::cout << "LifetimeSanitizer - Done using: " << this << " : " << m_name << std::endl;
152+
}
153153
if (m_token != SANITIZER_TOKEN || m_self != this){
154154
std::cerr << "Done using corrupted: " << this << " : " << m_name << std::endl;
155155
terminate_with_dump();

SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ void SnapshotManager::convert(uint64_t seqnum, QVideoFrame frame, WallClock time
6565

6666
m_active_conversions--;
6767
m_cv.notify_all();
68+
69+
if (m_queued_convert){
70+
m_queued_convert = false;
71+
seqnum = m_cache.get_latest(frame, timestamp);
72+
if (m_converting_seqnum < seqnum){
73+
dispatch_conversion(seqnum, std::move(frame), timestamp);
74+
}
75+
}
6876
}
6977
bool SnapshotManager::try_dispatch_conversion(uint64_t seqnum, QVideoFrame frame, WallClock timestamp) noexcept{
7078
// Must call under the lock.
@@ -84,17 +92,6 @@ bool SnapshotManager::try_dispatch_conversion(uint64_t seqnum, QVideoFrame frame
8492
try{
8593
std::function<void()> lambda = [=, this, frame = std::move(frame)](){
8694
convert(seqnum, std::move(frame), timestamp);
87-
88-
std::lock_guard<std::mutex> lg(m_lock);
89-
if (m_queued_convert){
90-
m_queued_convert = false;
91-
QVideoFrame frame;
92-
WallClock timestamp;
93-
uint64_t seqnum = m_cache.get_latest(frame, timestamp);
94-
if (m_converting_seqnum < seqnum){
95-
dispatch_conversion(seqnum, std::move(frame), timestamp);
96-
}
97-
}
9895
};
9996

10097
*task = GlobalThreadPools::realtime_inference().try_dispatch(lambda);
@@ -106,9 +103,9 @@ bool SnapshotManager::try_dispatch_conversion(uint64_t seqnum, QVideoFrame frame
106103

107104
// Dispatch failed. Queue it for later.
108105
m_queued_convert = true;
109-
}catch (...){
110-
m_pending_conversions.erase(seqnum);
111-
}
106+
}catch (...){}
107+
108+
m_pending_conversions.erase(seqnum);
112109

113110
return false;
114111
}

0 commit comments

Comments
 (0)