AsyncTask: Use a mutex to guard access to AsyncTask.status_line.

While a backup or restore task was running, the status_line property
would be updated for each file processed.

Meanwhile, the gui (BackupBox, for example) was periodically
accessing the status_line property to set a label.

Without protection, the gui was picking up garbage from status_line
instead of the filename, and was flooding output with markup warnings.
There was probably potential for a crash as well.
This commit is contained in:
Michael Webster 2023-03-21 20:53:24 -04:00
parent 240225d547
commit 18cd48ddd4
No known key found for this signature in database
GPG Key ID: 9E8DD70A52A44C58

View File

@ -55,7 +55,10 @@ public abstract class AsyncTask : GLib.Object{
// public
public AppStatus status;
public string status_line = "";
private string _status_line = "";
public GLib.Mutex status_line_mutex;
public int exit_code = 0;
public string error_msg = "";
public GLib.Timer timer;
@ -73,6 +76,32 @@ public abstract class AsyncTask : GLib.Object{
public signal void stderr_line_read(string line);
public signal void task_complete();
[CCode(notify = false)]
public string status_line
{
owned get {
return _get_status_line();
}
set
{
status_line_mutex.lock();
_status_line = value;
status_line_mutex.unlock();
}
}
private string _get_status_line() {
string ret = "";
if (status_line_mutex.trylock()) {
ret = _status_line;
status_line_mutex.unlock();
}
return ret;
}
protected AsyncTask(){
working_dir = TEMP_DIR + "/" + timestamp_for_path();
@ -80,7 +109,8 @@ public abstract class AsyncTask : GLib.Object{
log_file = path_combine(working_dir, "task.log");
//regex = new Gee.HashMap<string,Regex>(); // needs to be initialized again in instance constructor
status_line_mutex = GLib.Mutex();
dir_create(working_dir);
}