Skip to content

Commit ce0c211

Browse files
Add --http_port flag to localserv and update tests
Added support for specifying a fixed HTTP port in localserv via the --http_port flag. Updated localserv_test.py to use this flag and find a free port via Python socket binding, removing the Unix-specific os.pipe and pass_fds logic to improve Windows compatibility.
1 parent 5b7f054 commit ce0c211

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

src/localserv/localserv_main.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static struct llama_context* create_context(struct llama_model* model, const Cha
163163
}
164164

165165
static void print_usage(const char* argv0) {
166-
fprintf(stderr, "usage: %s --model <model_path> [--context_length <n>] [--o_http_port <filepath>]\n", argv0);
166+
fprintf(stderr, "usage: %s --model <model_path> [--context_length <n>] [--o_http_port <filepath>] [--http_port <port>]\n", argv0);
167167
}
168168

169169
static std::string extract_json_string(const std::string& json, const std::string& key) {
@@ -201,6 +201,7 @@ int main(int argc, char** argv) {
201201
GlobalScope global_scope;
202202
ChatOptions opt;
203203
std::string o_http_port_filepath;
204+
int opt_http_port = -1;
204205

205206
int argi = 1;
206207
while (argi < argc) {
@@ -225,6 +226,13 @@ int main(int argc, char** argv) {
225226
return 1;
226227
}
227228
o_http_port_filepath = argv[argi];
229+
} else if (strcmp(argv[argi], "--http_port") == 0) {
230+
argi += 1;
231+
if (argi == argc) {
232+
print_usage(argv[0]);
233+
return 1;
234+
}
235+
opt_http_port = std::stoi(argv[argi]);
228236
} else {
229237
print_usage(argv[0]);
230238
return 1;
@@ -267,7 +275,9 @@ int main(int argc, char** argv) {
267275
int prev_len = 0;
268276

269277
int port = 8080;
270-
if (!o_http_port_filepath.empty()) {
278+
if (opt_http_port != -1) {
279+
port = opt_http_port;
280+
} else if (!o_http_port_filepath.empty()) {
271281
port = 0;
272282
}
273283

test/localserv/localserv_test.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,23 @@ def main():
2323
print(f"Error: Could not find src directory: {src_dir}")
2424
sys.exit(1)
2525

26-
# Create pipe for port communication
27-
r_fd, w_fd = os.pipe()
28-
port_arg = f"/dev/fd/{w_fd}"
26+
# Find a free port
27+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28+
s.bind(('localhost', 0))
29+
port = s.getsockname()[1]
30+
s.close()
31+
print(f"Selected port {port}")
2932

3033
# Start the server
31-
cmd = [executable_path] + extra_args + ["--o_http_port", port_arg]
34+
cmd = [executable_path] + extra_args + ["--http_port", str(port)]
3235
print(f"Starting server: {cmd} in {src_dir}")
3336
process = subprocess.Popen(
3437
cmd,
3538
cwd=src_dir,
3639
stdout=sys.stdout,
3740
stderr=sys.stderr,
38-
pass_fds=(w_fd,),
3941
)
4042

41-
# Close write end in parent
42-
os.close(w_fd)
43-
44-
# Read port from pipe
45-
try:
46-
with os.fdopen(r_fd, 'r') as f:
47-
port_str = f.readline().strip()
48-
if not port_str:
49-
raise ValueError("Failed to read port from pipe")
50-
port = int(port_str)
51-
print(f"Server listening on port {port}")
52-
except Exception as e:
53-
print(f"Failed to read port: {e}")
54-
process.terminate()
55-
sys.exit(1)
56-
5743
# Wait for server to start up (connect to the dynamic port)
5844
start_time = time.time()
5945
while time.time() - start_time < 20:

0 commit comments

Comments
 (0)