Skip to content

Commit

Permalink
Fix python thread
Browse files Browse the repository at this point in the history
Found by: Empus
Patch by: thommey and michaelortmann

Fixes [20:20:20] !!! writing to nonexistent socket: 9 when using putlog() or other functions using sockets not available via thread local storage returned via threaddata() in python thread

the underlying idea is that the dccsockets etc. of the main thread are separated from the one in Tcl threads because that is necessary.
From Python however, it should be (famous last words) safe to access the main thread's socket table, so this PR makes it globally accessible, with only Tcl threads having their own.
The safety requirement is that Python is only called when the main thread isn't operating on them and right now, Python is only called during Tcl binds and in select().
  • Loading branch information
michaelortmann authored Dec 1, 2024
1 parent caa08ed commit 14c181b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/tcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,14 @@ int tclthreadmainloop(int zero)
return (i == -5);
}

struct threaddata *td_main = 0;

struct threaddata *threaddata()
{
static Tcl_ThreadDataKey tdkey;
struct threaddata *td = Tcl_GetThreadData(&tdkey, sizeof(struct threaddata));
if (!(td->mainloopfunc) && td_main) /* python thread */
return td_main;
return td;
}

Expand All @@ -638,6 +642,8 @@ void init_threaddata(int mainthread)
td->blocktime.tv_usec = 0;
td->MAXSOCKS = 0;
increase_socks_max();
if (mainthread)
td_main = td;
}

/* workaround for Tcl that does not support unicode outside BMP (3 byte utf-8 characters) */
Expand Down

0 comments on commit 14c181b

Please sign in to comment.