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

Remove all 'exit' stmts, use 'throw exception' instead #3764

Closed
wants to merge 2 commits into from
Closed

Remove all 'exit' stmts, use 'throw exception' instead #3764

wants to merge 2 commits into from

Conversation

ab-chesspad
Copy link

The main goal is to build SF as a shared library, so it can be used without restrictions on Android and other platforms.
This is the 1st commit to implement the idea that a server or a method should never crash or stop without the explicit request from the caller.

@lucasart
Copy link

lucasart commented Nov 1, 2021

As far as I know, uncaught C++ exceptions cause the program to terminate, same as calling exit(EXIT_FAILURE). I don't think you can launch SF from parent process (like a GUI for example), and have the parent process catch the exceptions launched by SF.

@ab-chesspad
Copy link
Author

@lucasart You are right, but:

  1. The main difference is that 'exit' terminates the whole program immediately, while exception can be caught and the calling code can decide what to do with the error.
  2. When I submitted my full changes 3 weeks ago, people thought that they were too invasive and recommended me to split them into smaller, more manageable parts. So this is only the 1st one and yes, I intend to catch exceptions.

@vondele
Copy link
Member

vondele commented Nov 1, 2021

I've triggered another round of CI, previously it was failing. Not sure if that failure is related.

This change would need testing on fishtest to see if there is a performance impact of enabling exceptions. Personally, I think it is natural to have them enabled for C++ code, it is for example also needed for the cluster branch.

@ab-chesspad
Copy link
Author

ab-chesspad commented Nov 1, 2021 via email

@vondele
Copy link
Member

vondele commented Nov 1, 2021

the test passed now after the restarting CI. I think it was a CI hickup, maybe timeout. The logs (one can see by clicking on the CI details link) showed it failed in the 'reprosearch' part, but that can hardly be influenced by this patch.

I think next test is run this on fishtest verifying no regression.

@lucasart
Copy link

lucasart commented Nov 2, 2021

Probably begnign, but wouldn't this introduce scrambled output? If a thread is doing a bunch of sync_cout / sync_endl (holding a custom mutex), while the compiler generated code handles uncaught exceptions by printing them to stderr (or stdout?), hence to screen in a terminal, even if it does hold a mutex for that (does it?), it will be a different mutex. Now, we're talking about a fatal error case, so perhaps scrambled output there is fine.

To be pedantically correct, perhaps we should catch the exceptions in main, and print the error message using sync_cout.

@ab-chesspad
Copy link
Author

ab-chesspad commented Nov 2, 2021 via email

@dsmsgms
Copy link
Contributor

dsmsgms commented Nov 2, 2021

Is it having exceptions necessary for the Stockfish program itself, I don't like it at all

@ddobbelaere
Copy link
Contributor

As far as I understand, the so-called zero-cost implementation used in modern compilers (and ABIs) implies no runtime overhead as long as no exception is thrown.

See e.g. https://www.google.com/amp/s/mortoray.com/2013/09/12/the-true-cost-of-zero-cost-exceptions/amp/

@lucasart
Copy link

lucasart commented Nov 2, 2021

The hypothetical cost of compiling with exceptions can be measured (bench averaging or fishtest).

But I don't understand the point of this. Suppose you want to do away with UCI and compile engines as libraries (.so or .dll). With an API to be defined, why would it make sense to throw exceptions inside SF to catch them in the code that calls the library. Wouldn't that make the TBD library protocol dependant on the language (C++) and the compiler used to build the library ?

@ddobbelaere
Copy link
Contributor

ddobbelaere commented Nov 2, 2021

Wouldn't that make the TBD library protocol dependant on the language (C++) and the compiler used to build the library ?

That's why people invented the concept ABI, to make this thing work.

EDIT: apparently, like @Sopel97 mentions, this is not recommended as it enforces that all depending programs are compiled the same way (and would exclude languages that don't support exceptions). Catching those exceptions and converting them to error codes in the TBD library API part seems advisable.

@Sopel97
Copy link
Member

Sopel97 commented Nov 2, 2021

If this is not a regression I'm definitely for it. Exceptions should be zero-cost on the happy path, and only increase the binary size, which is already large.

But I don't understand the point of this. Suppose you want to do away with UCI and compile engines as libraries (.so or .dll). With an API to be defined, why would it make sense to throw exceptions inside SF to catch them in the code that calls the library. Wouldn't that make the TBD library protocol dependant on the language (C++) and the compiler used to build the library ?

It's possible to do an extern "C" api, which has stable and well defined ABI. I suspect that's what the OP wants to do. Exceptions will allow returning an error code instead of crashing the program that imported the library. Throwing exceptions across ABI boudaries is a big no no

@ppigazzini
Copy link
Contributor

@ab-chesspad you should reschedule the test using "Non-regression STC" bounds
https://tests.stockfishchess.org/tests/view/618078f059e71df00dcc4291
image

@BM123499
Copy link
Contributor

BM123499 commented Nov 2, 2021

I've started a test here. I'll delete pending ones for now.

@lucasart
Copy link

lucasart commented Nov 4, 2021

Should we litter the code with nothrow to make sure that the cost is really zero? At least on the hot path.

@ab-chesspad
Copy link
Author

ab-chesspad commented Nov 4, 2021 via email

@BM123499
Copy link
Contributor

BM123499 commented Nov 4, 2021

@ab-chesspad, unfortunately, your changes fail to pass non-regression test, as shown here: https://tests.stockfishchess.org/tests/view/6181974fe3bcfb9eb3a1aa7b .
So it's believed that this PR is causing a slowdown on SF.

@ab-chesspad
Copy link
Author

ab-chesspad commented Nov 4, 2021 via email

@BM123499
Copy link
Contributor

BM123499 commented Nov 4, 2021

@ab-chesspad
image
after 141520 games, fishtest calculate that this PR loses 0.78 elo. As it doesn't change functionally, it seems it's causing a slowdown.

@ab-chesspad
Copy link
Author

I thing I guessed it. The problem is not 'throw exception' itself (I don't believe it is), but -fexceptions flag (correct me if I am wrong).
So, unless we decide to sacrifice .78 loss of ELO, we cannot make SF more robust and user-friendly.

@vdbergh
Copy link
Contributor

vdbergh commented Nov 4, 2021

Note that the -0.78 elo comes with a CI that includes 0.

IMHO for non-functional changes it is better to do a speed test than a non-regression test, especially if it concerns architectural changes that people like (non-regression tests have a fair chance of failing a neutral patch). If this PR really loses 0.78 elo then this should be detectable by a speed test.

@vdbergh
Copy link
Contributor

vdbergh commented Nov 4, 2021

From this test #3323 (comment) one can make a guess about the conversion time/UHO_Elo. It seems that 1% speed loss should amount to a loss of approximatively 1 UHO_Elo.

@vondele
Copy link
Member

vondele commented Jun 7, 2022

so, this didn't pass CI and is now quite out-dated. I'm somehow sympathetic to the idea of making SF more easily callable as a library, but we can't regress for making that happen. Consequently, I'll close the PR.

@vondele vondele closed this Jun 7, 2022
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 this pull request may close these issues.

None yet

10 participants