Gaviota TB crash when probing from multiple threads

Discussion of chess software programming and technical issues.

Moderator: Ras

petero2
Posts: 724
Joined: Mon Apr 19, 2010 7:07 pm
Location: Sweden
Full name: Peter Osterlund

Gaviota TB crash when probing from multiple threads

Post by petero2 »

I recently discovered that there is a bug in the Gaviota tablebase probing code that can cause an engine using the probing code to crash if it probes from multiple threads. If you compile using a C11 capable compiler, the fix is simple:

Code: Select all

diff --git a/gtb-probe.c b/gtb-probe.c
index dbb612c..b4aab11 100755
--- a/gtb-probe.c
+++ b/gtb-probe.c
@@ -2033,7 +2033,7 @@ egtb_get_id (SQ_CONTENT *w, SQ_CONTENT *b, tbkey_t *id)
        char *t;
        bool_t found;
        tbkey_t i;
-       static tbkey_t cache_i = 0;
+       static _Thread_local tbkey_t cache_i = 0;
 
        assert (PAWN == 1 && KNIGHT == 2 && BISHOP == 3 && ROOK == 4 && QUEEN == 5 && KING == 6);
 
Maybe no engine still in development, other than Texel, is using Gaviota TBs though.
mar
Posts: 2655
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Gaviota TB crash when probing from multiple threads

Post by mar »

thanks Peter. I believe Myrddin uses GTB too
User avatar
towforce
Posts: 12367
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK
Full name: Graham Laight

Re: Gaviota TB crash when probing from multiple threads

Post by towforce »

Ideally, requests to the TB would be via a service architecture for several reasons - including this one. However, I do understand that this would be bad for performance, which is not acceptable in a chess engine.
Human chess is partly about tactics and strategy, but mostly about memory
Daniel Shawul
Posts: 4186
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Gaviota TB crash when probing from multiple threads

Post by Daniel Shawul »

I recently had a similar bug in Scorpio where I had the random number seed as static in the game generation code

Code: Select all

thread_local std::mt19937 mtgen(std::random_device{}());
Not sure how much it effect it had though.
JVMerlino
Posts: 1397
Joined: Wed Mar 08, 2006 10:15 pm
Location: San Francisco, California

Re: Gaviota TB crash when probing from multiple threads

Post by JVMerlino »

mar wrote: Wed Jun 25, 2025 8:58 am thanks Peter. I believe Myrddin uses GTB too
It does, but since I use processes instead of threads, it's not an issue. But I've modified my code anyway just to be sure.
tmokonen
Posts: 1356
Joined: Sun Mar 12, 2006 6:46 pm
Location: Kelowna
Full name: Tony Mokonen

Re: Gaviota TB crash when probing from multiple threads

Post by tmokonen »

Apparently _Thread_local is deprecated in C23. thread_local is now a keyword, rather than a convenience macro.

https://en.cppreference.com/w/c/keyword/_Thread_local