FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codegraph/__tests__/daemon-socket-fallback.test.ts at main · SiliconLabsSoftware/codegraph · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SiliconLabsSoftware
/
codegraph
Public
forked from
colbymchenry/codegraph
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codegraph
/
__tests__
/
daemon-socket-fallback.test.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
246 lines (223 loc) · 11.1 KB
Breadcrumbs
codegraph
/
__tests__
/
daemon-socket-fallback.test.ts
Copy path
File metadata and controls
246 lines (223 loc) · 11.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Daemon support on socket-incapable filesystems — issue #997 (and the adjacent
* #974 WSL2 DrvFs hazard).
*
* A project on an ExFAT/FAT external volume (or some network mounts / WSL2 DrvFs)
* breaks the daemon at TWO points, BOTH surfacing as ENOTSUP (verified on a real
* macOS fskit ExFAT volume):
*
* 1. Lock acquisition `link()`s a temp file onto `.codegraph/daemon.pid` for
* race-free exclusivity (#411). ExFAT has no hard links, so this throws
* first — before the socket is ever reached. The fix falls back to an
* O_EXCL create (`acquireLockViaExclusiveOpen`).
* 2. The socket `listen()` then throws ENOTSUP regardless of path length, so
* the old length-only tmpdir fallback never triggered. The fix makes the
* socket path an ORDERED candidate list (in-project, then a deterministic
* tmpdir path); the daemon binds the first that works and the proxy connects
* the first that answers, so both converge on the fallback with zero
* coordination.
*
* Both failures report a DIFFERENT errno per OS — ENOTSUP (macOS), EPERM (Linux),
* EISDIR (Windows) — so the fix deliberately does NOT gate on an enumerated set:
* the lock falls back on ANY non-EEXIST link error, the socket relocates on ANY
* non-EADDRINUSE bind error. These tests pin that policy (incl. a deliberately
* unanticipated errno), the candidate list, the candidate-walk binder, and the
* exclusive-open lock primitive. (Throwaway scripts drove the full daemon end-to-
* end on a real macOS ExFAT image, a Linux FAT loopback mount, and a Windows
* exFAT VHD — relocate, serve a real client, rewrite the pidfile — none of which
* can run in CI.)
*/
import
{
afterEach
,
describe
,
expect
,
it
}
from
'vitest'
;
import
*
as
fs
from
'fs'
;
import
*
as
net
from
'net'
;
import
*
as
os
from
'os'
;
import
*
as
path
from
'path'
;
import
{
getDaemonPidPath
,
getDaemonSocketCandidates
,
getDaemonSocketPath
,
}
from
'../src/mcp/daemon-paths'
;
import
type
{
DaemonLockInfo
}
from
'../src/mcp/daemon-paths'
;
import
{
decodeLockInfo
}
from
'../src/mcp/daemon-paths'
;
import
{
acquireLockViaExclusiveOpen
,
bindFirstUsableSocket
,
tryAcquireDaemonLock
,
}
from
'../src/mcp/daemon'
;
const
POSIX
=
process
.
platform
!==
'win32'
;
const
tmpFiles
:
string
[
]
=
[
]
;
const
tmpDirs
:
string
[
]
=
[
]
;
afterEach
(
(
)
=>
{
while
(
tmpFiles
.
length
)
{
try
{
fs
.
rmSync
(
tmpFiles
.
pop
(
)
!
,
{
force
:
true
}
)
;
}
catch
{
/* best-effort */
}
}
while
(
tmpDirs
.
length
)
{
try
{
fs
.
rmSync
(
tmpDirs
.
pop
(
)
!
,
{
recursive
:
true
,
force
:
true
}
)
;
}
catch
{
/* best-effort */
}
}
}
)
;
/** A stand-in net.Server — bindFirstUsableSocket only ever passes it through. */
const
fakeServer
=
(
tag
:
string
)
:
net
.
Server
=>
(
{
tag
}
as
unknown
as
net
.
Server
)
;
/** Build an ErrnoException carrying a specific code, like a real listen() error. */
function
errno
(
code
:
string
)
:
NodeJS
.
ErrnoException
{
const
e
=
new
Error
(
`listen
${
code
}
`
)
as
NodeJS
.
ErrnoException
;
e
.
code
=
code
;
return
e
;
}
describe
(
'getDaemonSocketCandidates (#997)'
,
(
)
=>
{
it
.
runIf
(
POSIX
)
(
'returns [in-project, tmpdir] for a normal short path'
,
(
)
=>
{
const
root
=
path
.
join
(
os
.
tmpdir
(
)
,
'cg-cand-short'
)
;
const
candidates
=
getDaemonSocketCandidates
(
root
)
;
expect
(
candidates
)
.
toHaveLength
(
2
)
;
expect
(
candidates
[
0
]
)
.
toBe
(
path
.
join
(
root
,
'.codegraph'
,
'daemon.sock'
)
)
;
expect
(
candidates
[
1
]
!
.
startsWith
(
os
.
tmpdir
(
)
)
)
.
toBe
(
true
)
;
expect
(
path
.
basename
(
candidates
[
1
]
!
)
)
.
toMatch
(
/
^
c
o
d
e
g
r
a
p
h
-
[
0
-
9
a
-
f
]
{
16
}
\.
s
o
c
k
$
/
)
;
}
)
;
it
.
runIf
(
POSIX
)
(
'drops straight to [tmpdir] when the in-project path is too long'
,
(
)
=>
{
// A deep root pushes `.codegraph/daemon.sock` past the POSIX socket limit.
const
root
=
path
.
join
(
'/tmp'
,
'x'
.
repeat
(
120
)
)
;
const
candidates
=
getDaemonSocketCandidates
(
root
)
;
expect
(
candidates
)
.
toHaveLength
(
1
)
;
expect
(
candidates
[
0
]
!
.
startsWith
(
os
.
tmpdir
(
)
)
)
.
toBe
(
true
)
;
}
)
;
it
.
runIf
(
POSIX
)
(
'is deterministic and project-scoped: same root → same tmpdir fallback'
,
(
)
=>
{
const
root
=
path
.
join
(
os
.
tmpdir
(
)
,
'cg-cand-determinism'
)
;
const
a
=
getDaemonSocketCandidates
(
root
)
;
const
b
=
getDaemonSocketCandidates
(
root
)
;
expect
(
a
)
.
toEqual
(
b
)
;
// A different root yields a different (hashed) tmpdir fallback.
const
other
=
getDaemonSocketCandidates
(
root
+
'-other'
)
;
expect
(
other
[
other
.
length
-
1
]
)
.
not
.
toBe
(
a
[
a
.
length
-
1
]
)
;
}
)
;
it
.
runIf
(
!
POSIX
)
(
'returns a single named pipe on Windows'
,
(
)
=>
{
const
candidates
=
getDaemonSocketCandidates
(
'C:/dev/proj'
)
;
expect
(
candidates
)
.
toHaveLength
(
1
)
;
expect
(
candidates
[
0
]
!
.
startsWith
(
'\\\\.\\pipe\\codegraph-'
)
)
.
toBe
(
true
)
;
}
)
;
it
(
'getDaemonSocketPath returns the preferred candidate (index 0)'
,
(
)
=>
{
const
root
=
path
.
join
(
os
.
tmpdir
(
)
,
'cg-cand-primary'
)
;
expect
(
getDaemonSocketPath
(
root
)
)
.
toBe
(
getDaemonSocketCandidates
(
root
)
[
0
]
)
;
}
)
;
}
)
;
describe
(
'bindFirstUsableSocket (#997)'
,
(
)
=>
{
it
(
'binds the first candidate when it works, without relocating'
,
async
(
)
=>
{
const
tried
:
string
[
]
=
[
]
;
const
relocations
:
string
[
]
=
[
]
;
const
result
=
await
bindFirstUsableSocket
(
[
'/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
]
,
(
p
)
=>
{
tried
.
push
(
p
)
;
return
Promise
.
resolve
(
fakeServer
(
p
)
)
;
}
,
{
onRelocate
:
(
from
,
to
)
=>
relocations
.
push
(
`
${
from
}
->
${
to
}
`
)
}
,
)
;
expect
(
result
.
socketPath
)
.
toBe
(
'/proj/.codegraph/daemon.sock'
)
;
expect
(
tried
)
.
toEqual
(
[
'/proj/.codegraph/daemon.sock'
]
)
;
// never touched the fallback
expect
(
relocations
)
.
toEqual
(
[
]
)
;
}
)
;
it
(
'relocates to the tmpdir fallback when the in-project bind throws ENOTSUP'
,
async
(
)
=>
{
const
tried
:
string
[
]
=
[
]
;
const
relocations
:
Array
<
[
string
,
string
,
string
]
>
=
[
]
;
const
result
=
await
bindFirstUsableSocket
(
[
'/exfat/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
]
,
(
p
)
=>
{
tried
.
push
(
p
)
;
if
(
p
.
includes
(
'/exfat/'
)
)
return
Promise
.
reject
(
errno
(
'ENOTSUP'
)
)
;
return
Promise
.
resolve
(
fakeServer
(
p
)
)
;
}
,
{
onRelocate
:
(
from
,
to
,
code
)
=>
relocations
.
push
(
[
from
,
to
,
code
]
)
}
,
)
;
expect
(
result
.
socketPath
)
.
toBe
(
'/tmp/fallback.sock'
)
;
expect
(
tried
)
.
toEqual
(
[
'/exfat/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
]
)
;
expect
(
relocations
)
.
toEqual
(
[
[
'/exfat/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
,
'ENOTSUP'
]
,
]
)
;
}
)
;
it
(
'does NOT relocate on EADDRINUSE — it propagates even with a fallback present'
,
async
(
)
=>
{
const
tried
:
string
[
]
=
[
]
;
await
expect
(
bindFirstUsableSocket
(
[
'/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
]
,
(
p
)
=>
{
tried
.
push
(
p
)
;
return
Promise
.
reject
(
errno
(
'EADDRINUSE'
)
)
;
}
,
)
,
)
.
rejects
.
toMatchObject
(
{
code
:
'EADDRINUSE'
}
)
;
expect
(
tried
)
.
toEqual
(
[
'/proj/.codegraph/daemon.sock'
]
)
;
// fallback never tried
}
)
;
it
(
'propagates a capability error on the LAST candidate (nowhere left to go)'
,
async
(
)
=>
{
// When tmpdir itself can't host a socket, the single-candidate long-path list
// (or the exhausted tail of a longer one) has no fallback — the daemon must
// surface the error so the launcher drops to direct mode (#974).
await
expect
(
bindFirstUsableSocket
(
[
'/tmp/only.sock'
]
,
(
)
=>
Promise
.
reject
(
errno
(
'ENOTSUP'
)
)
,
)
,
)
.
rejects
.
toMatchObject
(
{
code
:
'ENOTSUP'
}
)
;
}
)
;
it
(
'walks past multiple unusable candidates to the first that binds'
,
async
(
)
=>
{
const
tried
:
string
[
]
=
[
]
;
const
result
=
await
bindFirstUsableSocket
(
[
'/a.sock'
,
'/b.sock'
,
'/c.sock'
]
,
(
p
)
=>
{
tried
.
push
(
p
)
;
if
(
p
===
'/a.sock'
)
return
Promise
.
reject
(
errno
(
'ENOTSUP'
)
)
;
if
(
p
===
'/b.sock'
)
return
Promise
.
reject
(
errno
(
'EACCES'
)
)
;
return
Promise
.
resolve
(
fakeServer
(
p
)
)
;
}
,
)
;
expect
(
result
.
socketPath
)
.
toBe
(
'/c.sock'
)
;
expect
(
tried
)
.
toEqual
(
[
'/a.sock'
,
'/b.sock'
,
'/c.sock'
]
)
;
}
)
;
it
(
'relocates on an UNEXPECTED errno too — the policy is "anything but EADDRINUSE", not a fixed list'
,
async
(
)
=>
{
// ExFAT/FAT report different bind errnos per OS (ENOTSUP macOS, EPERM Linux),
// so we must NOT gate relocation on an enumerated set — a code we never
// anticipated must still fall through to tmpdir. 'EWEIRD' stands in for any
// such surprise.
const
result
=
await
bindFirstUsableSocket
(
[
'/odd/proj/.codegraph/daemon.sock'
,
'/tmp/fallback.sock'
]
,
(
p
)
=>
p
.
includes
(
'/odd/'
)
?
Promise
.
reject
(
errno
(
'EWEIRD'
)
)
:
Promise
.
resolve
(
fakeServer
(
p
)
)
,
)
;
expect
(
result
.
socketPath
)
.
toBe
(
'/tmp/fallback.sock'
)
;
}
)
;
}
)
;
describe
(
'lock acquisition without hard links (#997)'
,
(
)
=>
{
// The hard-link-FAILS path (link() → O_EXCL fallback) can't be forced on a
// normal FS — fs.linkSync's namespace export is non-configurable, so it can't
// be spied. It's proven instead end-to-end on real ExFAT/FAT/exFAT volumes
// (macOS ENOTSUP, Linux EPERM, Windows EISDIR — all acquire via the fallback).
// Here we just guard that the refactored catch block didn't break the normal
// link path: a clean acquire, and a second caller correctly sees it held.
it
.
runIf
(
POSIX
)
(
'tryAcquireDaemonLock still acquires on a normal FS, and a second caller is told it is taken'
,
(
)
=>
{
const
root
=
fs
.
mkdtempSync
(
path
.
join
(
os
.
tmpdir
(
)
,
'cg-lock-'
)
)
;
tmpDirs
.
push
(
root
)
;
const
first
=
tryAcquireDaemonLock
(
root
)
;
expect
(
first
.
kind
)
.
toBe
(
'acquired'
)
;
const
pidPath
=
getDaemonPidPath
(
root
)
;
expect
(
fs
.
existsSync
(
pidPath
)
)
.
toBe
(
true
)
;
expect
(
decodeLockInfo
(
fs
.
readFileSync
(
pidPath
,
'utf8'
)
)
?.
pid
)
.
toBe
(
process
.
pid
)
;
const
second
=
tryAcquireDaemonLock
(
root
)
;
// link() → EEXIST → taken
expect
(
second
.
kind
)
.
toBe
(
'taken'
)
;
if
(
second
.
kind
===
'taken'
)
expect
(
second
.
existing
?.
pid
)
.
toBe
(
process
.
pid
)
;
}
)
;
it
.
runIf
(
POSIX
)
(
'acquireLockViaExclusiveOpen creates the pidfile with a complete, parseable record'
,
(
)
=>
{
const
pidPath
=
path
.
join
(
os
.
tmpdir
(
)
,
`cg-excl-
${
process
.
pid
}
-
${
Date
.
now
(
)
}
.pid`
)
;
tmpFiles
.
push
(
pidPath
)
;
const
info
:
DaemonLockInfo
=
{
pid
:
4242
,
version
:
'9.9.9-test'
,
socketPath
:
'/tmp/whatever.sock'
,
startedAt
:
1_700_000_000_000
,
}
;
const
acquired
=
acquireLockViaExclusiveOpen
(
pidPath
,
info
)
;
expect
(
acquired
)
.
toBe
(
true
)
;
// The file is non-empty and decodes back to exactly what we wrote — i.e. no
// empty-file window left behind for a reader to mistake for a corrupt lock.
expect
(
decodeLockInfo
(
fs
.
readFileSync
(
pidPath
,
'utf8'
)
)
)
.
toEqual
(
info
)
;
}
)
;
it
.
runIf
(
POSIX
)
(
'acquireLockViaExclusiveOpen is exclusive: the second caller loses (EEXIST → false)'
,
(
)
=>
{
const
pidPath
=
path
.
join
(
os
.
tmpdir
(
)
,
`cg-excl2-
${
process
.
pid
}
-
${
Date
.
now
(
)
}
.pid`
)
;
tmpFiles
.
push
(
pidPath
)
;
const
winner
:
DaemonLockInfo
=
{
pid
:
1
,
version
:
'a'
,
socketPath
:
'/s1'
,
startedAt
:
1
}
;
const
loser
:
DaemonLockInfo
=
{
pid
:
2
,
version
:
'b'
,
socketPath
:
'/s2'
,
startedAt
:
2
}
;
expect
(
acquireLockViaExclusiveOpen
(
pidPath
,
winner
)
)
.
toBe
(
true
)
;
expect
(
acquireLockViaExclusiveOpen
(
pidPath
,
loser
)
)
.
toBe
(
false
)
;
// does not clobber
// The winner's record is intact — the loser never overwrote it.
expect
(
decodeLockInfo
(
fs
.
readFileSync
(
pidPath
,
'utf8'
)
)
)
.
toEqual
(
winner
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL