Fix GC deinit race leading to bogus leak reports. (#3865)

This commit is contained in:
Nikolay Igotti
2020-02-17 13:45:09 +03:00
committed by GitHub
parent 1618e19b8d
commit 0425826fe8
2 changed files with 11 additions and 3 deletions
+1 -1
View File
@@ -169,7 +169,7 @@ class CyclicCollector {
restartCount = 0;
restart:
COLLECTOR_LOG("start cycle GC\n");
if (restartCount > 10) {
if (restartCount > 10 && !terminateCollector_) {
COLLECTOR_LOG("wait for some time to avoid GC trashing\n");
struct timeval tv;
struct timespec ts;
+10 -2
View File
@@ -1748,11 +1748,18 @@ MemoryState* initMemory() {
}
void deinitMemory(MemoryState* memoryState) {
static int pendingDeinit = 0;
atomicAdd(&pendingDeinit, 1);
#if USE_GC
bool lastMemoryState = atomicAdd(&aliveMemoryStatesCount, -1) == 0;
bool checkLeaks = g_checkLeaks && lastMemoryState;
if (lastMemoryState) {
garbageCollect(memoryState, true);
#if USE_CYCLIC_GC
// If there are other pending deinits (rare situation) - just skip the leak checker.
if (atomicGet(&pendingDeinit) > 1) {
checkLeaks = false;
}
cyclicDeinit();
#endif // USE_CYCLIC_GC
}
@@ -1770,9 +1777,10 @@ void deinitMemory(MemoryState* memoryState) {
konanDestructInstance(memoryState->tlsMap);
RuntimeAssert(memoryState->finalizerQueue == nullptr, "Finalizer queue must be empty");
RuntimeAssert(memoryState->finalizerQueueSize == 0, "Finalizer queue must be empty");
#endif // USE_GC
atomicAdd(&pendingDeinit, -1);
#if TRACE_MEMORY
if (IsStrictMemoryModel && lastMemoryState && allocCount > 0) {
MEMORY_LOG("*** Memory leaks, leaked %d containers ***\n", allocCount);
@@ -1780,7 +1788,7 @@ void deinitMemory(MemoryState* memoryState) {
}
#else
#if USE_GC
if (IsStrictMemoryModel && lastMemoryState && allocCount > 0 && g_checkLeaks) {
if (IsStrictMemoryModel && allocCount > 0 && checkLeaks) {
char buf[1024];
konan::snprintf(buf, sizeof(buf),
"Memory leaks detected, %d objects leaked!\n"