widgets(terminal): prevent deadlock in draw

The draw method could obtain a lock but never free it due to the logic
of the if statement.
This commit is contained in:
Tim Culverhouse 2024-06-09 21:04:36 -05:00
parent 3a382eacdb
commit 11ae2e7b29

View file

@ -166,9 +166,12 @@ pub fn resize(self: *Terminal, ws: Winsize) !void {
} }
pub fn draw(self: *Terminal, win: vaxis.Window) !void { pub fn draw(self: *Terminal, win: vaxis.Window) !void {
if (self.back_mutex.tryLock() and !self.mode.sync) { if (self.back_mutex.tryLock()) {
defer self.back_mutex.unlock(); defer self.back_mutex.unlock();
try self.back_screen.copyTo(&self.front_screen); // We keep this as a separate condition so we don't deadlock by obtaining the lock but not
// having sync
if (!self.mode.sync)
try self.back_screen.copyTo(&self.front_screen);
} }
var row: usize = 0; var row: usize = 0;