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

Error when trying to use timers #235

Open
Taneb opened this issue Dec 20, 2021 · 4 comments
Open

Error when trying to use timers #235

Taneb opened this issue Dec 20, 2021 · 4 comments

Comments

@Taneb
Copy link

Taneb commented Dec 20, 2021

I'm trying to implement something using SDL timers, but I was getting a segfault whenever the timer first triggered. I've got this down to a fairly small failing example:

import Control.Monad (forever)
import qualified SDL

main :: IO ()
main = do
  SDL.initialize [SDL.InitTimer]
  ticker <- SDL.addTimer 1000 $ \d -> putStrLn "tick" >> return (SDL.Reschedule d)
  forever $ pure ()

This fails with

sdl-experiment: schedule: re-entered unsafely.
   Perhaps a 'foreign import unsafe' should be 'safe'?

This also occurs if I try to use the raw API, but does not occur if I try to port this to C.

Note that in my original larger example, I only got Segmentation fault (core dumped) as the error message, so there might be more than one thing going on here.

GHC 8.10.7, sdl2 Haskell library version 2.5.3.0, SDL2 C library 2.0.14, NixOS

@ocharles
Copy link
Member

Oops, that's no good! Would you be able to try GHC's suggestion of changing the FFI imports to be safe? I imagine something addTimer uses is causes this re-entrance

@Taneb
Copy link
Author

Taneb commented Dec 20, 2021

I can't find anything called schedule. Explicitly marking safe all the FFI calls that addTimer seems to use (addTimerFFI, removeTimerFFI, mkTimerCallback) doesn't seem to help at all.

@ocharles
Copy link
Member

Hmmm, possibly initialize itself then? Other than that, it can only imagine it's how we create the callback, but I'll have to get into the code to understand that

@Taneb
Copy link
Author

Taneb commented Dec 26, 2021

I'm not sure why this fixes it, but having the timer write to an MVar seems to fix the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants