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

Stockfish crashes on invalid position instead of error mesasge #97

Closed
mjg1000 opened this issue Jun 2, 2022 · 5 comments · Fixed by #98
Closed

Stockfish crashes on invalid position instead of error mesasge #97

mjg1000 opened this issue Jun 2, 2022 · 5 comments · Fixed by #98

Comments

@mjg1000
Copy link

mjg1000 commented Jun 2, 2022

When stockfish is given a position where both kings are in check, such as 1q2nB2/pP1k2KP/NN1Q1qP1/8/1P1p4/4p1br/3R4/6n1/ w, stockfish will simply crash. Is there a way to instead of crashing throwing an error message, as then it would be possible to use a try: except: statement to filter out these positions?

@kieferro
Copy link
Contributor

kieferro commented Jun 2, 2022

I would also like to have an error message for invalid FENs that can be caught.
I think a change to the _read_line-function like this would work:

def _read_line(self) -> str:
    if not self._stockfish.stdout:
        raise BrokenPipeError()
    if self._stockfish.poll() is not None:
        raise ValueError(
            "Stockfish has crashed. This was probably caused by an invalid FEN."
        )
    return self._stockfish.stdout.readline().strip()

What do you think @zhelyabuzhsky? I could add that, some checks with invalid FENs and an entry in the README and then open a PR if you want this feature to be included.

@johndoknjas
Copy link
Contributor

@kieferro I think that would be good to include. An invalid FEN can cause SF to either crash, or return (none) for the best move (in both cases, after giving it the "go" command). It looks like your code handles the first case. I found some code online which should be able to help out with detecting fens causing this case as well. I'm also writing some stuff for fens causing the second case. Our branches shouldn't conflict since we're dealing with different functions.

@kieferro
Copy link
Contributor

kieferro commented Jun 2, 2022

@johndoknjas Okay, I've already included my code and a unittest on my fork. Feel free to take care of the second case. As long as both cases return the same kind of error, everything should be fine.
Do you want me to make the change to the README as well? I would just add a section and write that when an invalid FEN is entered, a ValueError with the text "Stockfish has crashed. This was probably caused by an invalid FEN." gets raised and maybe explain how to catch it. Does this fit with what you are planning on doing?

Edit: Now that I think about it, it probably does make sense to have two different error messages. If I understand correctly, then after the check you are dealing with, it is certain that the FEN is invalid. However, I only check if the process has been terminated, which also happens, for example, if someone kills the process. Therefore it would probably make sense to at least formulate the text differently. What are your thoughts?

@johndoknjas
Copy link
Contributor

@kieferro I think your error message makes sense, since the process just stopping has a good chance of being caused by a bad FEN. Although in the readme, you could say that it's not guaranteed invalid FENs will cause a crash. The stuff I'm doing more or less tries to deal with these kinds of FENs. So I think what you have fits with my code, and updating the readme is fine. My PR will involve some stuff other than fen validation, so I'll probably wait until yours is merged into master first. As far as error messages go, right now my plan for my function is to just return a bool instead, and then say in the readme that the user can call this function to test their FEN. For your code though, returning an error should be done, since generally if read_line is called, errors aren't the expected behaviour.

@kieferro
Copy link
Contributor

kieferro commented Jun 3, 2022

@johndoknjas Yes, that makes sense. Thank you

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

Successfully merging a pull request may close this issue.

3 participants