Skip to content

Commit 6decbdf

Browse files
emailweixureyoung
authored andcommitted
Fix potential dead lock in PyDataProvider2 (#140)
This bug occasionally causes dead lock in test_RecurrentGradientMachine In general, conditional_variable::notify should be used together with mutex for changing condition.
1 parent 4615c51 commit 6decbdf

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

paddle/gserver/dataproviders/PyDataProvider2.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,15 @@ class PyDataProvider2 : public DataProvider {
377377
std::swap(callingContexts_[cid], callingContexts_[0]);
378378
cid = 0;
379379
}
380+
381+
PyObjectPtr front;
382+
{
383+
std::unique_lock<std::mutex> l(mtx_);
384+
front = pop_get_front(callingContexts_);
385+
}
380386
{
381387
PyGuard g;
382-
callingContexts_.pop_front();
388+
front.reset();
383389
}
384390
this->pullCV_.notify_all();
385391
continue;
@@ -411,10 +417,7 @@ class PyDataProvider2 : public DataProvider {
411417
poolActualSize_ += additionalBatchSize;
412418
dataPool_.emplace_back(data);
413419
}
414-
415-
{
416-
pullCV_.notify_all();
417-
}
420+
pullCV_.notify_all();
418421
}
419422
DBG << "load thread end";
420423
}

paddle/utils/Logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void installFailureWriter(void(*callback)(const char*, int));
191191
}
192192
#endif // PADDLE_USE_GLOG
193193

194-
#ifndef NDEBUG
194+
#ifdef NDEBUG
195195
#define DEBUG_LEVEL 5
196196
#define DBG VLOG(DEBUG_LEVEL)
197197
#else

paddle/utils/Util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ static bool contains(const Container& container, const T& val) {
112112
return std::find(container.begin(), container.end(), val) != container.end();
113113
}
114114

115+
/**
116+
* pop and get the front element of a container
117+
*/
118+
template<typename Container>
119+
typename Container::value_type pop_get_front(Container& c) {
120+
typename Container::value_type v;
121+
swap(v, c.front());
122+
c.pop_front();
123+
return v;
124+
}
125+
115126
#define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
116127

117128
/**

0 commit comments

Comments
 (0)