Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
mcpproxy-go/scripts/test-config-race.sh at main · AdamMagued/mcpproxy-go · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
AdamMagued
mcpproxy-go
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
mcpproxy-go
/
scripts
/
test-config-race.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
320 lines (274 loc) · 8.36 KB
main
Breadcrumbs
mcpproxy-go
/
scripts
/
test-config-race.sh
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
320 lines (274 loc) · 8.36 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!
/bin/bash
#
#
test-config-race.sh - Reproduce config file race condition
#
#
This script attempts to reproduce the race condition where:
#
1. Config file is being written (truncated then written)
#
2. Core process starts and reads the partially written file
#
3. Core fails with "invalid character '}'" JSON parse error
#
#
Expected result WITHOUT fix: ~5% failure rate (corrupted config reads)
#
Expected result WITH atomic writes: 0% failure rate
#
set
-e
#
Colors for output
RED=
'
\033[0;31m
'
GREEN=
'
\033[0;32m
'
YELLOW=
'
\033[1;33m
'
NC=
'
\033[0m
'
#
No Color
#
Configuration
ITERATIONS=
${1
:-
100}
TEST_DIR=
"
/tmp/mcpproxy-race-test-
$$
"
CONFIG_FILE=
"
$TEST_DIR
/mcp_config.json
"
RESULTS_FILE=
"
$TEST_DIR
/results.txt
"
#
Counters
SUCCESS_COUNT=0
PARTIAL_READ_COUNT=0
PARSE_ERROR_COUNT=0
echo
"
==================================================
"
echo
"
Config File Race Condition Reproduction Test
"
echo
"
==================================================
"
echo
"
"
echo
"
Test directory:
$TEST_DIR
"
echo
"
Iterations:
$ITERATIONS
"
echo
"
"
#
Create test directory
mkdir -p
"
$TEST_DIR
"
#
Create a sample config (large enough to have observable write time)
create_large_config
() {
local
file=
$1
cat
>
"
$file
"
<<
'
EOF
'
{
"listen": "127.0.0.1:8080",
"data_dir": "~/.mcpproxy",
"api_key": "test-api-key-1234567890",
"enable_web_ui": true,
"top_k": 5,
"tools_limit": 15,
"tool_response_limit": 20000,
"mcpServers": [
{
"name": "server-1",
"url": "http://localhost:8001",
"protocol": "http",
"enabled": true,
"quarantined": false
},
{
"name": "server-2",
"url": "http://localhost:8002",
"protocol": "http",
"enabled": true,
"quarantined": false
},
{
"name": "server-3",
"url": "http://localhost:8003",
"protocol": "http",
"enabled": true,
"quarantined": false
},
{
"name": "server-4",
"url": "http://localhost:8004",
"protocol": "http",
"enabled": true,
"quarantined": false
},
{
"name": "server-5",
"url": "http://localhost:8005",
"protocol": "http",
"enabled": true,
"quarantined": false
}
]
}
EOF
}
#
Simulate non-atomic write (current implementation)
non_atomic_write
() {
local
file=
$1
local
data=
$2
#
This mimics os.WriteFile behavior: truncate then write
#
The file is in a partial state during the write
echo
-n
"
$data
"
>
"
$file
"
}
#
Simulate atomic write (proposed fix)
atomic_write
() {
local
file=
$1
local
data=
$2
local
tmp_file=
"
${file}
.tmp.
$$
"
#
Write to temp file
echo
-n
"
$data
"
>
"
$tmp_file
"
#
Fsync (force to disk)
if
command
-v fsync
&
>
/dev/null
;
then
fsync
"
$tmp_file
"
elif
[[
"
$OSTYPE
"
==
"
darwin
"
*
]]
;
then
#
macOS doesn't have fsync command, use sync
sync
fi
#
Atomic rename
mv
"
$tmp_file
"
"
$file
"
}
#
Reader process that tries to read and parse JSON
read_and_validate
() {
local
file=
$1
local
iteration=
$2
#
Try to read the file
if
[
!
-f
"
$file
"
]
;
then
echo
"
ITERATION
$iteration
: File not found
"
>>
"
$RESULTS_FILE
"
return
1
fi
local
content=
$(
cat
"
$file
"
)
#
Check if content looks incomplete
local
char_count=
$(
echo -n
"
$content
"
|
wc -c
|
tr -d
'
'
)
if
[
"
$char_count
"
-lt
100 ]
;
then
echo
"
ITERATION
$iteration
: PARTIAL READ (only
$char_count
chars)
"
>>
"
$RESULTS_FILE
"
echo
"
$content
"
>>
"
$RESULTS_FILE
"
echo
"
---
"
>>
"
$RESULTS_FILE
"
return
2
fi
#
Try to parse as JSON
if
!
echo
"
$content
"
|
jq
.
>
/dev/null
2>&1
;
then
echo
"
ITERATION
$iteration
: JSON PARSE ERROR
"
>>
"
$RESULTS_FILE
"
echo
"
$content
"
>>
"
$RESULTS_FILE
"
echo
"
---
"
>>
"
$RESULTS_FILE
"
return
3
fi
echo
"
ITERATION
$iteration
: SUCCESS
"
>>
"
$RESULTS_FILE
"
return
0
}
#
Test function
run_test
() {
local
use_atomic=
$1
local
iteration=
$2
#
Generate new config content
local
config_content
config_content=
$(
cat
<<
EOF
{
"listen": "127.0.0.1:8080",
"data_dir": "~/.mcpproxy",
"api_key": "test-key-iteration-
$iteration
",
"enable_web_ui": true,
"top_k": 5,
"tools_limit": 15,
"tool_response_limit": 20000,
"mcpServers": [
{"name": "server-
$iteration
-1", "url": "http://localhost:8001", "enabled": true},
{"name": "server-
$iteration
-2", "url": "http://localhost:8002", "enabled": true},
{"name": "server-
$iteration
-3", "url": "http://localhost:8003", "enabled": true},
{"name": "server-
$iteration
-4", "url": "http://localhost:8004", "enabled": true},
{"name": "server-
$iteration
-5", "url": "http://localhost:8005", "enabled": true}
]
}
EOF
)
#
Start writer in background
if
[
"
$use_atomic
"
=
"
true
"
]
;
then
atomic_write
"
$CONFIG_FILE
"
"
$config_content
"
&
else
non_atomic_write
"
$CONFIG_FILE
"
"
$config_content
"
&
fi
local
writer_pid=
$!
#
Immediately try to read (race condition)
#
Add tiny delay to simulate core startup time
sleep 0.001
read_and_validate
"
$CONFIG_FILE
"
"
$iteration
"
local
result=
$?
#
Wait for writer to finish
wait
$writer_pid
2>
/dev/null
||
true
return
$result
}
#
Run tests with NON-ATOMIC writes
echo
"
Running test with NON-ATOMIC writes (current implementation)...
"
echo
"
This should show race condition failures (~5% of the time)
"
echo
"
"
>
"
$RESULTS_FILE
"
for
i
in
$(
seq 1
$ITERATIONS
)
;
do
if
[
$((
i
%
10
))
-eq
0 ]
;
then
echo
-n
"
.
"
fi
run_test
"
false
"
"
$i
"
result=
$?
case
$result
in
0)
((
SUCCESS_COUNT
++
))
;;
2)
((
PARTIAL_READ_COUNT
++
))
;;
3)
((
PARSE_ERROR_COUNT
++
))
;;
esac
done
echo
"
"
echo
"
"
echo
"
==================================================
"
echo
"
Results (NON-ATOMIC writes):
"
echo
"
==================================================
"
echo
-e
"
${GREEN}
Successful reads:
$SUCCESS_COUNT
/
$ITERATIONS
${NC}
"
echo
-e
"
${YELLOW}
Partial reads:
$PARTIAL_READ_COUNT
/
$ITERATIONS
${NC}
"
echo
-e
"
${RED}
Parse errors:
$PARSE_ERROR_COUNT
/
$ITERATIONS
${NC}
"
echo
"
"
TOTAL_FAILURES=
$((
PARTIAL_READ_COUNT
+
PARSE_ERROR_COUNT
))
FAILURE_RATE=
$((
TOTAL_FAILURES
*
100
/
ITERATIONS
))
if
[
$TOTAL_FAILURES
-gt
0 ]
;
then
echo
-e
"
${RED}
⚠️ RACE CONDITION DETECTED!
${NC}
"
echo
-e
"
${RED}
Failure rate:
${FAILURE_RATE}
% (
$TOTAL_FAILURES
failures in
$ITERATIONS
attempts)
${NC}
"
echo
"
"
echo
"
Sample failures saved to:
$RESULTS_FILE
"
echo
"
"
echo
"
First 3 failures:
"
grep -A 5
"
PARTIAL READ\|PARSE ERROR
"
"
$RESULTS_FILE
"
|
head -n 20
else
echo
-e
"
${GREEN}
✓ No race condition detected
${NC}
"
echo
"
(Note: Race conditions are probabilistic - try increasing iterations)
"
fi
echo
"
"
echo
"
==================================================
"
#
Now test with ATOMIC writes
echo
"
"
echo
"
Running test with ATOMIC writes (proposed fix)...
"
echo
"
This should show NO failures
"
echo
"
"
SUCCESS_COUNT=0
PARTIAL_READ_COUNT=0
PARSE_ERROR_COUNT=0
>
"
$RESULTS_FILE
"
for
i
in
$(
seq 1
$ITERATIONS
)
;
do
if
[
$((
i
%
10
))
-eq
0 ]
;
then
echo
-n
"
.
"
fi
run_test
"
true
"
"
$i
"
result=
$?
case
$result
in
0)
((
SUCCESS_COUNT
++
))
;;
2)
((
PARTIAL_READ_COUNT
++
))
;;
3)
((
PARSE_ERROR_COUNT
++
))
;;
esac
done
echo
"
"
echo
"
"
echo
"
==================================================
"
echo
"
Results (ATOMIC writes):
"
echo
"
==================================================
"
echo
-e
"
${GREEN}
Successful reads:
$SUCCESS_COUNT
/
$ITERATIONS
${NC}
"
echo
-e
"
${YELLOW}
Partial reads:
$PARTIAL_READ_COUNT
/
$ITERATIONS
${NC}
"
echo
-e
"
${RED}
Parse errors:
$PARSE_ERROR_COUNT
/
$ITERATIONS
${NC}
"
echo
"
"
TOTAL_FAILURES=
$((
PARTIAL_READ_COUNT
+
PARSE_ERROR_COUNT
))
if
[
$TOTAL_FAILURES
-gt
0 ]
;
then
echo
-e
"
${RED}
⚠️ UNEXPECTED FAILURES WITH ATOMIC WRITES!
${NC}
"
echo
-e
"
${RED}
This should not happen - atomic writes should prevent all race conditions
${NC}
"
exit
1
else
echo
-e
"
${GREEN}
✓ No race condition detected with atomic writes
${NC}
"
echo
-e
"
${GREEN}
✓ All
$ITERATIONS
reads were successful
${NC}
"
fi
echo
"
"
echo
"
==================================================
"
echo
"
Test complete!
"
echo
"
==================================================
"
#
Cleanup
rm -rf
"
$TEST_DIR
"
echo
"
"
echo
"
Summary:
"
echo
"
- Non-atomic writes are susceptible to race conditions
"
echo
"
- Atomic writes (temp + rename) prevent all race conditions
"
echo
"
- This reproduces the issue reported in GitHub issue #86
"
You can’t perform that action at this time.