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

Tables with __tostring metamethods lead to infinite yield #273

Open
CompeyDev opened this issue Nov 2, 2024 · 0 comments · May be fixed by #288
Open

Tables with __tostring metamethods lead to infinite yield #273

CompeyDev opened this issue Nov 2, 2024 · 0 comments · May be fixed by #288
Labels
enhancement New feature or request

Comments

@CompeyDev
Copy link
Contributor

CompeyDev commented Nov 2, 2024

Summary

With tables that have a __tostring metamethod that invokes stdio.format, a mutex deadlock within the formatter is triggered, causing an infinite yield.

Minimal Reproduction

local stdio = require("@lune/stdio")

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

print(stdio.format(5)) -- prints immediately
print(a) -- yields infinitely

Root Cause

This behavior occurs due to the mutex guard here:

let _guard = COLORS_LOCK.lock().unwrap();

This guard exists to prevent sudden changes in global coloring configuration in the console crate, which can cause unexpected coloring behavior.

The print method calls the formatter, which calls a __tostring implementation if present, and the metamethod calls stdio.format again -- bam! Infinite recursion.

Potential Fixes

  • Trivially replace the Mutex with an RwLock and prevent this deadlock
  • File an issue with console-rs to make color configuration non-global
@filiptibell filiptibell added the enhancement New feature or request label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants