Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock in stdio.format calls in tostring metamethod #288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/lune-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dunce = "1.0"
once_cell = "1.17"
path-clean = "1.0"
pathdiff = "0.2"
parking_lot = "0.12.3"
12 changes: 5 additions & 7 deletions crates/lune-utils/src/fmt/value/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
collections::HashSet,
sync::{Arc, Mutex},
};
use std::{collections::HashSet, sync::Arc};

use console::{colors_enabled as get_colors_enabled, set_colors_enabled};
use mlua::prelude::*;
use once_cell::sync::Lazy;
use parking_lot::ReentrantMutex;

mod basic;
mod config;
Expand All @@ -20,15 +18,15 @@ pub use self::config::ValueFormatConfig;
// NOTE: Since the setting for colors being enabled is global,
// and these functions may be called in parallel, we use this global
// lock to make sure that we don't mess up the colors for other threads.
static COLORS_LOCK: Lazy<Arc<Mutex<()>>> = Lazy::new(|| Arc::new(Mutex::new(())));
static COLORS_LOCK: Lazy<Arc<ReentrantMutex<()>>> = Lazy::new(|| Arc::new(ReentrantMutex::new(())));

/**
Formats a Lua value into a pretty string using the given config.
*/
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn pretty_format_value(value: &LuaValue, config: &ValueFormatConfig) -> String {
let _guard = COLORS_LOCK.lock().unwrap();
let _guard = COLORS_LOCK.lock();

let were_colors_enabled = get_colors_enabled();
set_colors_enabled(were_colors_enabled && config.colors_enabled);
Expand All @@ -48,7 +46,7 @@ pub fn pretty_format_value(value: &LuaValue, config: &ValueFormatConfig) -> Stri
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn pretty_format_multi_value(values: &LuaMultiValue, config: &ValueFormatConfig) -> String {
let _guard = COLORS_LOCK.lock().unwrap();
let _guard = COLORS_LOCK.lock();

let were_colors_enabled = get_colors_enabled();
set_colors_enabled(were_colors_enabled && config.colors_enabled);
Expand Down
20 changes: 20 additions & 0 deletions tests/stdio/format.luau
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ stdio.ewrite(typeof(errorMessage))
assertContains("Should format errors similarly to userdata", stdio.format(errorMessage), "<LuaErr")
assertContains("Should format errors with stack begins", stdio.format(errorMessage), "Stack Begin")
assertContains("Should format errors with stack ends", stdio.format(errorMessage), "Stack End")

-- Check that calling stdio.format in a __tostring metamethod by print doesn't cause a deadlock

local inner = {}
setmetatable(inner, {
__tostring = function()
return stdio.format(5)
end,
})

print(inner)

local outer = {}
setmetatable(outer, {
__tostring = function()
return stdio.format(inner)
end,
})

print(outer)
Loading