From 2880301d69c9ffadba8615d4d2bddcf2246d3e76 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 4 Mar 2021 18:02:48 -0500 Subject: [PATCH] Issue #450 - Updated the runtime to avoid a race condition when creating new symbols --- CHANGELOG.md | 1 + runtime.c | 11 +++++++++-- scheme/cyclone/cgen.sld | 6 ------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8334977b..f4b499a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Features Bug Fixes +- Updated the runtime to avoid a race condition when creating new symbols. Thanks to Skye Soss for the bug report and patch. - Prevent the compiler from inlining calls to primitives that open ports, avoiding a range of issues such as an open file operation being inlined across multiple places in the intermediate code. - Arthur Maciel updated `current-jiffy` to use `clock_gettime`. diff --git a/runtime.c b/runtime.c index 705bfc08..6a6c7d46 100644 --- a/runtime.c +++ b/runtime.c @@ -454,9 +454,16 @@ static object find_symbol_by_name(const char *name) object add_symbol(symbol_type * psym) { pthread_mutex_lock(&symbol_table_lock); // Only 1 "writer" allowed - set_insert(&symbol_table, psym); + bool inserted = set_insert(&symbol_table, psym); pthread_mutex_unlock(&symbol_table_lock); - return psym; + if (!inserted) { + object sym = find_symbol_by_name(psym->desc); + free((char *)(psym->desc)); + free(psym); + return sym; + } else { + return psym; + } } static object add_symbol_by_name(const char *name) diff --git a/scheme/cyclone/cgen.sld b/scheme/cyclone/cgen.sld index 3fffa9a0..bb0d0466 100644 --- a/scheme/cyclone/cgen.sld +++ b/scheme/cyclone/cgen.sld @@ -2162,12 +2162,6 @@ *globals*) (emit "") - ;; Initialize symbol table - (for-each - (lambda (sym) - (emit* " add_symbol(quote_" (mangle sym) ");")) - *symbols*) - ;; Initialize globals (let* ((prefix " ") (emit-global