AllureOfTheStars / Allure

Allure of the Stars is a near-future Sci-Fi roguelike and tactical squad combat game written in Haskell; please offer feedback, e.g., after trying out the web frontend version at
http://allureofthestars.com
GNU Affero General Public License v3.0
211 stars 16 forks source link

Crash on Mac #79

Closed PabloReszczynski closed 6 years ago

PabloReszczynski commented 6 years ago

After installing the game with cabal install allure I'm getting the following exception when running the game:

2018-04-29 23:17:16.520 Allure[69743:3115685] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called
from the Main Thread!'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fffd016257b __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fffe53c61da objc_exception_throw + 48
        2   AppKit                              0x00007fffce34be82 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 4480
        3   libSDL2-2.0.0.dylib                 0x0000000104f92247 Cocoa_PumpEvents + 211
        4   libSDL2-2.0.0.dylib                 0x0000000104f16893 SDL_PumpEvents_REAL + 23
        5   libSDL2-2.0.0.dylib                 0x0000000104f1690e SDL_WaitEventTimeout_REAL + 76
        6   Allure                              0x00000001027b124d sdl2zm2zi4zi0zi1zmD0fGQCRnSosDzzIattq3RAB_SDLziEvent_pollEvent1_info + 7597
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1]    69743 abort      Allure

I'm running macOS Sierra 10.12.6 with GHC 8.0.2 and Cabal 2.2.0.1

Mikolaj commented 6 years ago

Thanks a lot for the report. Will investigate. It's not the first time the lack of thread safety of SDL has bitten me, so as a battle-scarred veteran, I stand a chance. :) Can you confirm (e.g., by running cabal with -v2 and posting the log, if you'd be so kind) that you are installing version 0.7.1.0 of Allure? Sometimes dependencies force older versions, or cabal update was not performed first. If you feel adventurous, you can also try installing from github repo (first LambdaHack, then Allure), though you'd also need to add at least --force-reinstalls for it for work on GHC < 8.4 (or alternatively, install two more packages by hand from repos for it to work on GHC >= 8.4). I will let you know when I have a possible fix.

PabloReszczynski commented 6 years ago

Allure --version gives me 0.7.1.0. So will try with the git versions next. Should I checkout on 0.7.1.0? Or a newer tag?

Mikolaj commented 6 years ago

Thank you for the confirmation. Trying out the master branch gives the highest chance of success, though not a very large one (the SDL thread safety problems were fixed already 0.7.1.0 or so I thought). The master branch is essentially a finished 0.8.0.0 release, so your bug report is very timely to prevent embarrassing crashes with a new release.

If you are using GHC 8.0.2, do cabal install --force-reinstalls first in LambdaHack repo, then Allure.

PabloReszczynski commented 6 years ago

Okay so the 0.7.1.0 branches don't fix the problem and gives the same error. Gonna try now with master branch.

Mikolaj commented 6 years ago

If you have a moment after trying out the master branch, a simple hack to try is to replace (on master branch) the line

SDL.initialize [SDL.InitVideo, SDL.InitEvents]

with

SDL.initializeAll

and see if the crash is gone. That's the only cheap idea that I have, without honest debugging.

PabloReszczynski commented 6 years ago

Couldn't build LambdaHack. This was the error:

cabal: Error: some packages failed to install:
LambdaHack-0.8.0.0-Ky05fh6cNIMIhLCyQ5hP6R depends on LambdaHack-0.8.0.0 which
failed to install.
hsini-0.5.1.1-8Ghfe4G6uhy5rUIxKZutTV failed during the configure step. The
exception was:
dieVerbatim: user error (cabal: '/usr/local/bin/ghc' exited with an error:
<command line>: cannot satisfy -package-id
Cabal-2.2.0.1-BDWwpOuNAGoKFyVU5uJpBC:
Cabal-2.2.0.1-BDWwpOuNAGoKFyVU5uJpBC is unusable due to missing or recursive
dependencies:
text-1.2.3.0-6xzJvl4muXO89DtSnMQyzH parsec-3.1.13.0-Czo1znv1tMHI9xwYay5uF
(use -v for more information)
)

Maybe is it my cabal?

PabloReszczynski commented 6 years ago

And this is what cabal configure throws:

❯ cabal configure
Resolving dependencies...
Warning: solver failed to find a solution:
Could not resolve dependencies:
[__0] trying: LambdaHack-0.8.0.0 (user goal)
[__1] trying: LambdaHack:-jsaddle
[__2] trying: LambdaHack:-vty
[__3] trying: LambdaHack:-curses
[__4] trying: LambdaHack:-gtk
[__5] next goal: sdl2-ttf (dependency of LambdaHack -curses -gtk -jsaddle
-vty)
[__5] rejecting: sdl2-ttf-2.1.0/installed-6zZ... (package is broken)
After searching the rest of the dependency tree exhaustively, these were the
goals I've had most trouble fulfilling: LambdaHack, LambdaHack:curses,
LambdaHack:gtk, LambdaHack:jsaddle, LambdaHack:vty, sdl2-ttf
Trying configure anyway.
Configuring LambdaHack-0.8.0.0...
Error:
    The following packages are broken because other packages they depend on are missing. These broken packages must be rebuilt before they can be used.
installed package adjunctions-4.4 is broken due to missing package profunctors-5.2.2-AiAeaQano6CJg6xLb90ygw
installed package async-2.2.1 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr
installed package bytes-0.15.4 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr, scientific-0.3.5.3-FV7PvhwnKCx1i4XqgquYmg, text-1.2.3.0-70ldaoxeFu2DFOOnxfdBHd, unordered-containers-0.2.9.0-hI3gq8BZgbFLS7IhMpeSh
installed package free-5.0.2 is broken due to missing package profunctors-5.2.2-AiAeaQano6CJg6xLb90ygw
installed package hsini-0.5.1.1 is broken due to missing package parsec-3.1.13.0-LJyx3iKX2kdFBqf9Cns6Rf
installed package kan-extensions-5.1 is broken due to missing package profunctors-5.2.2-AiAeaQano6CJg6xLb90ygw
installed package keys-3.12 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr, unordered-containers-0.2.9.0-hI3gq8BZgbFLS7IhMpeSh
installed package lens-4.16.1 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr, profunctors-5.2.2-AiAeaQano6CJg6xLb90ygw, text-1.2.3.0-70ldaoxeFu2DFOOnxfdBHd, th-abstraction-0.2.6.0-HRFJgpoqs5HJ5LgHnjmMhQ, unordered-containers-0.2.9.0-hI3gq8BZgbFLS7IhMpeSh
installed package linear-1.20.7 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr, unordered-containers-0.2.9.0-hI3gq8BZgbFLS7IhMpeSh
installed package sdl2-2.4.0.1 is broken due to missing package text-1.2.3.0-70ldaoxeFu2DFOOnxfdBHd
installed package sdl2-ttf-2.1.0 is broken due to missing package text-1.2.3.0-70ldaoxeFu2DFOOnxfdBHd
installed package semigroupoids-5.2.2 is broken due to missing package hashable-1.2.7.0-3hg2TEghlsEJmDAYciLYpr, unordered-containers-0.2.9.0-hI3gq8BZgbFLS7IhMpeSh
Mikolaj commented 6 years ago

The second output is normal --- configure complains some packages are not installed (yet), because cabal install didn't succeed. The first output is puzzling --- it doesn't happen on CI. Could you post the whole output of cabal install --force-reinstalls -v as a git gist?

Mikolaj commented 6 years ago

@PabloReszczynski: I've removed the dependency on the newest containers package, so now an ordinary "cabal install" should just work on master branch. If if you still have any trouble, just remove ~/.cabal and ~/.ghc and all that's broken will be rebuilt fine.

Mikolaj commented 6 years ago

I've had another look at that. The message error "nextEventMatchingMask should only be called from the Main Thread!" suggests, this SDL implementation is extremely restrictive. Is there another SDL implementation for macOS that would not restrict me to the main thread? E.g., perhaps there is one based on OpenGL? If so, we could ship binary macOS releases with this implementation's library files and recommend it for development in README. Until I know that (and whether the SDL.initializeAll hack helps or not), I'm sticking to a single bound thread instead of the main thread: https://github.com/LambdaHack/LambdaHack/blob/54b50b05fce5ecb9e283091db4f861953a4555c4/Game/LambdaHack/Client/UI/Frontend/Sdl.hs#L74

PabloReszczynski commented 6 years ago

Sadly I dont know of any other sdl implementation than the official one at libsdlorg

Mikolaj commented 6 years ago

Frankly, I have no clue about SDL. So there may indeed be no alternative --- I have no idea from which level (SDL, OpenGL, graphics card drivers, OS internals, whatever) the silly restriction to use only the main thread comes and so if it can be side-stepped and with what effort.

What I know is I had once the frontend running in the main thread (with GTK) and it complicates the code and makes its topmost levels quite unreadable.

Did you try the SDL.initializeAll hack by any chance?

Mikolaj commented 6 years ago

This is a known issue:

https://trac.macports.org/ticket/54614#comment:10 https://github.com/opencv/opencv/issues/7791 https://github.com/hybridgroup/gobot/issues/545#issuecomment-391336130 https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-07/msg00398.html https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg04574.html

E.g.: "[...] discussion with the GLFW & gr-forphor lead devs. The consensus is to just not use GLFW fosphor on OSX because of this (artificial) constraint added by Apple. I will look into disabling this part of the gr-fosphor interface."

I wonder if it's reported upstream (wherever it is). Especially that it's said to be a regression starting with OS X Sierra (apparently located in Cocoa? is Cocoa free software? can it be patched by anyone and upgraded for free?).

Perhaps I should have a separate complex Main.hs (and, with luck, only a couple more modules) for OS X and so retain the readable simple code for the other OSes.

PabloReszczynski commented 6 years ago

Yeah, Apple abandoned opengl support for their Cocoa and Metal frameworks. And not, Cocoa is closed source. A separate Main for os x would be the way to go I think. I would happily contribute but my haskell knowledge is not very good.

Mikolaj commented 6 years ago

Thank you for your offer. Your help would be indispensable, given that I don't have access to a recent enough Mac. Are you able to compile current master branch? Either a trivial invocation of cabal or of stack should do.

PabloReszczynski commented 6 years ago

The master branch compiles just fine, for both Allure and LambdaHack

Mikolaj commented 6 years ago

Splendid. Could make one experiment for me? In the LambdaHack repo do

git grep SDL.initialize

and in the file it finds replace the line it finds with

SDL.initializeAll

Make sure to start the line with exactly the same number of spaces as the original line (whitespace matters in Haskell).

Then compile again, run it and check if it still crashes. That's the only hacky workaround I managed to google and if it fails, I just need to do it the messy way, with adding special cruft code for Mac.

PabloReszczynski commented 6 years ago

Did the change and compiled both, but got the same error

Mikolaj commented 6 years ago

Ok, thank you very much. So I will do it the hard way and then depend on your kindness to test if my fixes worked. No idea how long it's going to take, Will ping you. Thanks again!

Mikolaj commented 6 years ago

@PabloReszczynski: I've concocted a terrible, but cheap hack attempting to fix the crash, for now only in the LambdaHack repo. Could you try it at your leisure?

PabloReszczynski commented 6 years ago

Got a new error this time:

Allure: thread blocked indefinitely in an MVar operation

Also I had to edit the cabal file to allow the newer LambdaHack version

Mikolaj commented 6 years ago

Oh, that must be, because you tried Allure and I only tried to fix LambdaHack now. Could you run make play or just the LambdaHack executable in the LambdaHack repo? Allure is not supposed to be compilable now, until you verify the workaround works fine and I know I can keep it.

PabloReszczynski commented 6 years ago

make play works flawlessly!

Mikolaj commented 6 years ago

Great job! Let me then port the fix to Allure, write changelog, release and then it'll be your turn to supply Mac packages, if you will.

PabloReszczynski commented 6 years ago

Getting this error in the current master branch:

Contract failed and the following is to blame:
  ("RuleKind: some content items not valid",[(["rmainMenuArt has a line with length other than 110:                                                                                "],The game ruleset specification.)])
Allure: thread blocked indefinitely in an MVar operation
Mikolaj commented 6 years ago

Ouch, I told you the release is ready, forgot and proceeded to add stuff. :) And you caught the 2 repos in an inconsistent state. Please git pull now in both the LambdaHack and Allure repos and reinstall/rebuild accordingly.

PabloReszczynski commented 6 years ago

Now it works! Will build the .zip and send it to your mail address or upload it myself since I'm now a contributor.

Mikolaj commented 6 years ago

Amazing. That means the bug is fixed and I can close the issue. We did it!