Commit 496f8e2
FIXED: use GCD dispatch semaphores on macOS instead of named POSIX semaphores
macOS does not implement unnamed POSIX semaphores (sem_init() is a deprecated
ENOSYS stub), so this code used a named semaphore opened with a fixed name
"pl" via sem_open()/sem_unlink(). That name lives in a single machine-global
namespace, which has two failure modes when many short-lived Prolog processes
run concurrently:
- Two processes in the sem_open()..sem_unlink() window collide on
O_CREAT|O_EXCL and one gets EEXIST.
- A process killed between sem_open() and sem_unlink() leaves the name
registered, after which every later sem_open(O_EXCL) fails until reboot.
Both surface as hard-to-reproduce thread teardown ("threads wouldn't die")
and thread CPU-time failures on macOS, and also break under sandboxes that
deny ipc-posix-sem. The old failure path made it worse: my_sem_open() tested
the result against NULL, but sem_open() reports failure as SEM_FAILED, so a
failed open stored a broken handle and could exit(1) at startup.
Replace the named semaphores with GCD dispatch semaphores: in-process,
unnamed, with a native timed wait. dispatch_semaphore_signal() is a
lock-free atomic increment with a Mach semaphore_signal() trap on its slow
path -- no libc locks or malloc -- so it stays safe to call from the
SIG_SYNCTIME signal handler. The dispatch timed wait also replaces the
setitimer()-based grace timeout (USE_TIMER_WAIT), now unused on macOS, while
still honouring halt_grace_time and reporting threads that outlive it. If
dispatch_semaphore_create() ever fails (OOM), the halt path degrades to
polling thread status rather than aborting. The selecting macro is renamed
USE_SEM_OPEN -> USE_DISPATCH_SEM to match. Only the macOS paths change;
other platforms are unaffected.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 0526294 commit 496f8e2
3 files changed
Lines changed: 88 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
163 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
164 | 165 | | |
165 | 166 | | |
166 | 167 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
264 | | - | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
174 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
175 | 176 | | |
176 | 177 | | |
177 | 178 | | |
| |||
180 | 181 | | |
181 | 182 | | |
182 | 183 | | |
183 | | - | |
| 184 | + | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
195 | 202 | | |
196 | 203 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | 204 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
| 205 | + | |
| 206 | + | |
213 | 207 | | |
214 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
215 | 212 | | |
216 | 213 | | |
| 214 | + | |
217 | 215 | | |
218 | 216 | | |
219 | 217 | | |
220 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
221 | 225 | | |
222 | 226 | | |
223 | 227 | | |
| |||
1114 | 1118 | | |
1115 | 1119 | | |
1116 | 1120 | | |
1117 | | - | |
| 1121 | + | |
1118 | 1122 | | |
1119 | 1123 | | |
1120 | 1124 | | |
| |||
1214 | 1218 | | |
1215 | 1219 | | |
1216 | 1220 | | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
1217 | 1263 | | |
1218 | 1264 | | |
1219 | 1265 | | |
| |||
1277 | 1323 | | |
1278 | 1324 | | |
1279 | 1325 | | |
| 1326 | + | |
| 1327 | + | |
1280 | 1328 | | |
1281 | 1329 | | |
1282 | 1330 | | |
| |||
7663 | 7711 | | |
7664 | 7712 | | |
7665 | 7713 | | |
7666 | | - | |
7667 | | - | |
| 7714 | + | |
| 7715 | + | |
7668 | 7716 | | |
7669 | 7717 | | |
7670 | 7718 | | |
| |||
7747 | 7795 | | |
7748 | 7796 | | |
7749 | 7797 | | |
7750 | | - | |
| 7798 | + | |
| 7799 | + | |
| 7800 | + | |
| 7801 | + | |
7751 | 7802 | | |
7752 | 7803 | | |
7753 | 7804 | | |
| |||
0 commit comments