Skip to content

Commit 5863f46

Browse files
authored
Filelock path rectification (#321)
* filelock_path updated as /tmp folder Signed-off-by: Keval Mahajan <[email protected]> * filelock_path uses temp_dir bydefault and then appends to the path configured Signed-off-by: Keval Mahajan <[email protected]> * updated FILELOCK_NAME Signed-off-by: Keval Mahajan <[email protected]> * updated filename_path Signed-off-by: Keval Mahajan <[email protected]> --------- Signed-off-by: Keval Mahajan <[email protected]>
1 parent 55ef3ba commit 5863f46

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ UNHEALTHY_THRESHOLD=3
241241
#####################################
242242
# Lock file Settings
243243
#####################################
244-
FILELOCK_PATH=/tmp/gateway_healthcheck_init.lock
244+
245+
# This path is append with the system temp directory.
246+
# It is used to ensure that only one instance of the gateway health Check can run at a time.
247+
FILELOCK_NAME=gateway_healthcheck_init.lock # saved dir in /tmp/gateway_healthcheck_init.lock
248+
# FILELOCK_NAME=somefolder/gateway_healthcheck_init.lock # saved dir in /tmp/somefolder/gateway_healthcheck_init.lock#
245249

246250

247251
#####################################

charts/mcp-stack/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Kubernetes: `>=1.21.0`
5151
| mcpContextForge.config.FEDERATION_PEERS | string | `"[]"` | |
5252
| mcpContextForge.config.FEDERATION_SYNC_INTERVAL | string | `"300"` | |
5353
| mcpContextForge.config.FEDERATION_TIMEOUT | string | `"30"` | |
54-
| mcpContextForge.config.FILELOCK_PATH | string | `"/tmp/gateway_healthcheck_init.lock"` | |
54+
| mcpContextForge.config.FILELOCK_NAME | string | `"gateway_healthcheck_init.lock"` | |
5555
| mcpContextForge.config.GUNICORN_MAX_REQUESTS | string | `"10000"` | |
5656
| mcpContextForge.config.GUNICORN_MAX_REQUESTS_JITTER | string | `"100"` | |
5757
| mcpContextForge.config.GUNICORN_PRELOAD_APP | string | `"true"` | |

charts/mcp-stack/values.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,10 @@
521521
"description": "Unhealthy threshold",
522522
"default": "3"
523523
},
524-
"FILELOCK_PATH": {
524+
"FILELOCK_NAME": {
525525
"type": "string",
526526
"description": "File lock path",
527-
"default": "/tmp/gateway_healthcheck_init.lock"
527+
"default": "gateway_healthcheck_init.lock"
528528
},
529529
"DEV_MODE": {
530530
"type": "string",

charts/mcp-stack/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ mcpContextForge:
193193
HEALTH_CHECK_INTERVAL: "60" # seconds between peer health checks
194194
HEALTH_CHECK_TIMEOUT: "10" # request timeout per health check
195195
UNHEALTHY_THRESHOLD: "3" # failed checks before peer marked unhealthy
196-
FILELOCK_PATH: /tmp/gateway_healthcheck_init.lock # lock file used at start-up
196+
FILELOCK_NAME: gateway_healthcheck_init.lock # lock file used at start-up
197197

198198
# ─ Development toggles ─
199199
DEV_MODE: "false" # enable dev-mode features

mcpgateway/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _parse_federation_peers(cls, v):
169169
health_check_timeout: int = 10 # seconds
170170
unhealthy_threshold: int = 5 # after this many failures, mark as Offline
171171

172-
filelock_path: str = "tmp/gateway_service_leader.lock"
172+
filelock_name: str = "gateway_service_leader.lock"
173173

174174
# Default Roots
175175
default_roots: List[str] = []

mcpgateway/services/gateway_service.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import logging
2121
from typing import Any, AsyncGenerator, Dict, List, Optional, Set
2222
import uuid
23+
import tempfile
24+
import os
2325

2426
# Third-Party
2527
from filelock import FileLock, Timeout
@@ -123,7 +125,13 @@ def __init__(self) -> None:
123125
elif settings.cache_type != "none":
124126
# Fallback: File-based lock
125127
self._redis_client = None
126-
self._lock_path = settings.filelock_path
128+
129+
temp_dir = tempfile.gettempdir()
130+
user_path = os.path.normpath(settings.filelock_name)
131+
if os.path.isabs(user_path):
132+
user_path = os.path.relpath(user_path, start=os.path.splitdrive(user_path)[0] + os.sep)
133+
full_path = os.path.join(temp_dir, user_path)
134+
self._lock_path = full_path.replace("\\", "/")
127135
self._file_lock = FileLock(self._lock_path)
128136
else:
129137
self._redis_client = None

tests/unit/mcpgateway/cache/test_resource_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
Unit tests for ResourceCache.
99
"""
10+
1011
# Standard
1112
import asyncio
1213
import time

0 commit comments

Comments
 (0)