[geary/wip/logging-improvements: 2/3] Use a mutex in Geary.Logging.default_handler



commit 2717d26e4a89b5af39fc934d7b91f2a06af89773
Author: Michael Gratton <mike vee net>
Date:   Thu Jun 27 22:25:41 2019 +1000

    Use a mutex in Geary.Logging.default_handler
    
    The function is not otherwise threadsafe, and causes segfaults when
    e.g. `--log-sql` is enabled.

 src/engine/api/geary-logging.vala | 9 +++++++++
 1 file changed, 9 insertions(+)
---
diff --git a/src/engine/api/geary-logging.vala b/src/engine/api/geary-logging.vala
index a8b6c6c7..a77c2490 100644
--- a/src/engine/api/geary-logging.vala
+++ b/src/engine/api/geary-logging.vala
@@ -106,6 +106,8 @@ private Flag logging_flags = Flag.NONE;
 private unowned FileStream? stream = null;
 private Timer? entry_timer = null;
 
+// Can't be nullable. See https://gitlab.gnome.org/GNOME/vala/issues/812
+private GLib.Mutex record_lock;
 private Record? first_record = null;
 private Record? last_record = null;
 private uint log_length = 0;
@@ -124,6 +126,7 @@ public void init() {
     if (init_count++ != 0)
         return;
     entry_timer = new Timer();
+    record_lock = GLib.Mutex();
     max_log_length = DEFAULT_MAX_LOG_BUFFER_LENGTH;
 }
 
@@ -220,6 +223,10 @@ public void log_to(FileStream? stream) {
 public void default_handler(string? domain,
                             LogLevelFlags log_levels,
                             string message) {
+    // Obtain a lock since multiple threads can be calling this
+    // function at the same time
+    record_lock.lock();
+
     Record record = new Record(
         domain,
         log_levels,
@@ -264,6 +271,8 @@ public void default_handler(string? domain,
         out.puts(record.format());
         out.putc('\n');
     }
+
+    record_lock.unlock();
 }
 
 private inline string to_prefix(LogLevelFlags level) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]