Skip to content

Commit dafec04

Browse files
authored
Merge pull request DeusData#877 from Flipper1994/fix/798-full-ui-repro-harness
test(win): full UI-mode hang repro harness for DeusData#798 follow-up
2 parents 0488acf + 7a93a38 commit dafec04

3 files changed

Lines changed: 228 additions & 1 deletion

File tree

‎tests/test_httpd.c‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../src/foundation/log.h"
1717
#include "../src/foundation/platform.h"
1818
#include "../src/cli/cli.h"
19+
#include "../src/git/git_context.h" /* #798 follow-up: live-socket git-resolve repro */
1920
#include "../src/ui/http_server.h"
2021
#include "test_framework.h"
2122
#include "test_helpers.h"
@@ -34,13 +35,15 @@
3435
#ifdef _WIN32
3536
#include <winsock2.h>
3637
#include <ws2tcpip.h>
38+
#include <windows.h> /* #798 follow-up: CreateThread/WaitForSingleObject watchdog */
3739
typedef SOCKET th_sock_t;
3840
#define th_sock_close closesocket
3941
#define TH_SOCK_BAD INVALID_SOCKET
4042
#else
4143
#include <arpa/inet.h>
4244
#include <netinet/in.h>
4345
#include <sys/socket.h>
46+
#include <sys/time.h> /* struct timeval for the SO_RCVTIMEO watchdog (#798 follow-up) */
4447
#include <unistd.h>
4548
typedef int th_sock_t;
4649
#define th_sock_close close
@@ -962,6 +965,126 @@ TEST(repo_info_strips_credentials_from_remote) {
962965
PASS();
963966
}
964967

968+
/* ── #798 follow-up: full UI-mode hang repro (live sockets) ───── */
969+
970+
/* Like th_http but arms a client-side receive-timeout watchdog. If the
971+
* single-threaded server wedges, recv() returns instead of blocking forever, so
972+
* the test FAILs deterministically rather than hanging CI. 0 on connect/timeout. */
973+
static int th_http_deadline(int port, const char *request, char *resp, size_t respsz,
974+
int timeout_ms) {
975+
th_sock_t s = th_connect(port);
976+
if (s == TH_SOCK_BAD)
977+
return 0;
978+
#ifdef _WIN32
979+
DWORD tv = (DWORD)timeout_ms;
980+
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
981+
#else
982+
struct timeval tv;
983+
tv.tv_sec = timeout_ms / 1000;
984+
tv.tv_usec = (timeout_ms % 1000) * 1000;
985+
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
986+
#endif
987+
if (th_send_all(s, request, strlen(request)) != 0) {
988+
th_sock_close(s);
989+
return 0;
990+
}
991+
int n = th_recv_until_close(s, resp, respsz);
992+
th_sock_close(s);
993+
return n;
994+
}
995+
996+
/* #798 was a single-threaded-server wedge: list_projects never returned and the
997+
* whole UI stopped answering. Assert the running server answers list_projects
998+
* within a hard deadline while it holds live listening sockets. The client
999+
* receive-timeout is the watchdog: a wedge → no 200 → FAIL, never a CI hang. */
1000+
TEST(ui_server_list_projects_responds_under_watchdog) {
1001+
th_server_t ts;
1002+
ASSERT_EQ(th_server_start(&ts), 0);
1003+
const char *body = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","
1004+
"\"params\":{\"name\":\"list_projects\",\"arguments\":{}}}";
1005+
char req[512];
1006+
snprintf(req, sizeof(req),
1007+
"POST /rpc HTTP/1.1\r\n"
1008+
"Content-Type: application/json\r\n"
1009+
"Content-Length: %d\r\n\r\n%s",
1010+
(int)strlen(body), body);
1011+
char resp[8192];
1012+
int n = th_http_deadline(cbm_http_server_port(ts.srv), req, resp, sizeof(resp), 15000);
1013+
th_server_stop(&ts);
1014+
ASSERT_GT(n, 0); /* a response arrived before the watchdog fired */
1015+
ASSERT_EQ(th_status(resp), 200);
1016+
ASSERT_NOT_NULL(strstr(resp, "\"jsonrpc\""));
1017+
PASS();
1018+
}
1019+
1020+
#ifdef _WIN32
1021+
typedef struct {
1022+
char path[512];
1023+
int resolved_ok;
1024+
} th_gitctx_probe_t;
1025+
1026+
static DWORD WINAPI th_gitctx_probe_thread(LPVOID arg) {
1027+
th_gitctx_probe_t *p = (th_gitctx_probe_t *)arg;
1028+
cbm_git_context_t ctx;
1029+
memset(&ctx, 0, sizeof(ctx));
1030+
int rc = cbm_git_context_resolve(p->path, &ctx);
1031+
p->resolved_ok = (rc == 0 && ctx.is_git) ? 1 : 0;
1032+
cbm_git_context_free(&ctx);
1033+
return 0;
1034+
}
1035+
#endif
1036+
1037+
/* The load-bearing end-to-end repro of #798: while the single-threaded UI server
1038+
* holds LIVE listening/AFD socket handles in this process, cbm_git_context_resolve
1039+
* — the exact path list_projects runs (add_git_context_json → resolve →
1040+
* cbm_popen(git)) — must not hang. Under a raw-_popen regression git inherits
1041+
* those sockets and its MSYS2 runtime deadlocks in NtQueryObject; the watchdog
1042+
* turns that into a hard FAIL instead of an infinite hang. */
1043+
TEST(git_context_resolve_no_hang_under_live_ui_sockets) {
1044+
#ifndef _WIN32
1045+
SKIP_PLATFORM("Windows-only: #798 UI listening-socket handle inheritance");
1046+
#else
1047+
char *tmp = th_mktempdir("cbm_798repro");
1048+
if (!tmp)
1049+
FAIL("th_mktempdir returned NULL");
1050+
1051+
char cmd[1024];
1052+
snprintf(cmd, sizeof(cmd),
1053+
"git -C \"%s\" init -q && git -C \"%s\" -c user.email=t@t -c user.name=t "
1054+
"commit -q --allow-empty -m init",
1055+
tmp, tmp);
1056+
if (system(cmd) != 0) {
1057+
th_rmtree(tmp);
1058+
SKIP_PLATFORM("git not available to init a repo");
1059+
}
1060+
1061+
th_server_t ts;
1062+
ASSERT_EQ(th_server_start(&ts), 0);
1063+
1064+
th_gitctx_probe_t *probe = (th_gitctx_probe_t *)calloc(1, sizeof(*probe));
1065+
ASSERT_NOT_NULL(probe);
1066+
snprintf(probe->path, sizeof(probe->path), "%s", tmp);
1067+
1068+
HANDLE h = CreateThread(NULL, 0, th_gitctx_probe_thread, probe, 0, NULL);
1069+
ASSERT_NOT_NULL(h);
1070+
DWORD w = WaitForSingleObject(h, 30000);
1071+
if (w != WAIT_OBJECT_0) {
1072+
/* Wedged on the inherited-socket NtQueryObject walk. Deliberately leak
1073+
* the heap probe + thread (a late wake must not touch freed memory);
1074+
* process exit reaps them. Fail loudly rather than hang CI. */
1075+
th_server_stop(&ts);
1076+
FAIL("cbm_git_context_resolve hung under live UI sockets (#798 regression)");
1077+
}
1078+
CloseHandle(h);
1079+
th_server_stop(&ts);
1080+
int ok = probe->resolved_ok;
1081+
free(probe);
1082+
th_rmtree(tmp);
1083+
ASSERT_EQ(ok, 1);
1084+
PASS();
1085+
#endif
1086+
}
1087+
9651088
/* ── Suite ────────────────────────────────────────────────────── */
9661089

9671090
SUITE(httpd) {
@@ -1010,4 +1133,7 @@ SUITE(httpd) {
10101133
RUN_TEST(ui_server_slow_request_hits_deadline);
10111134
RUN_TEST(ui_server_access_log_redacts_query);
10121135
RUN_TEST(ui_server_stop_joins_cleanly);
1136+
/* #798 follow-up: full UI-mode hang repro under live sockets */
1137+
RUN_TEST(ui_server_list_projects_responds_under_watchdog);
1138+
RUN_TEST(git_context_resolve_no_hang_under_live_ui_sockets);
10131139
}

‎tests/test_main.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ int tf_skip_count = 0;
1616
#include "mcp/mcp.h" /* cbm_mcp_handle_tool — act as a real worker */
1717
#include <sqlite3.h>
1818
#include <stdbool.h>
19+
#include <stdint.h>
1920
#include <stdio.h>
2021
#include <stdlib.h>
2122
#include <string.h>
23+
#ifdef _WIN32
24+
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
25+
#endif
2226

2327
/* #832 guard support: when the index supervisor spawns THIS binary as
2428
* `<self> cli --index-worker index_repository <args_json> --response-out <file>`
@@ -77,6 +81,38 @@ static int tf_maybe_run_index_worker(int argc, char **argv) {
7781
return 0;
7882
}
7983

84+
/* #798 follow-up: socket-isolation probe. The parent test
85+
* (popen_isolates_listening_socket, test_security.c) spawns THIS binary through
86+
* cbm_popen — the same cmd.exe-grandchild path git takes — passing the numeric
87+
* value of an inheritable listening-socket handle. If cbm_popen correctly
88+
* isolates handles, that socket is NOT present in this child and getsockopt
89+
* fails; a regression to raw _popen leaks it (bInheritHandles=TRUE propagates it
90+
* transitively through cmd.exe) and getsockopt succeeds. We report via exit code
91+
* so the verdict survives `cmd.exe /c` (proven by popen_isolated_propagates_exit_code).
92+
* Returns an exit code (>=0) when it handled a probe invocation, else -1. */
93+
static int tf_maybe_run_socket_probe(int argc, char **argv) {
94+
#ifdef _WIN32
95+
if (argc < 3 || strcmp(argv[1], "__cbm_sockprobe") != 0) {
96+
return -1;
97+
}
98+
WSADATA wsa;
99+
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
100+
return 0; /* no winsock in child ⇒ cannot observe a socket ⇒ not leaked */
101+
}
102+
unsigned long long hv = strtoull(argv[2], NULL, 10);
103+
SOCKET s = (SOCKET)(uintptr_t)hv;
104+
int type = 0;
105+
int len = (int)sizeof(type);
106+
int rc = getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&type, &len);
107+
/* rc==0 ⇒ the handle is a live socket in THIS child ⇒ it was inherited. */
108+
return rc == 0 ? 42 : 0;
109+
#else
110+
(void)argc;
111+
(void)argv;
112+
return -1;
113+
#endif
114+
}
115+
80116
static int g_suite_argc = 0;
81117
static char **g_suite_argv = NULL;
82118

@@ -201,6 +237,13 @@ extern void suite_dump_verify_io(void);
201237
extern void cbm_kind_in_set_free_cache(void);
202238

203239
int main(int argc, char **argv) {
240+
/* #798 follow-up: if spawned as the socket-isolation probe, report whether an
241+
* inheritable socket handle crossed into this child and exit before any suite. */
242+
int probe_rc = tf_maybe_run_socket_probe(argc, argv);
243+
if (probe_rc >= 0) {
244+
return probe_rc;
245+
}
246+
204247
/* #832: if spawned as a supervised index worker, do the real work and exit
205248
* before any suite runs (see tf_maybe_run_index_worker). */
206249
int worker_rc = tf_maybe_run_index_worker(argc, argv);

‎tests/test_security.c‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
#ifdef _WIN32
1616
#include "../src/foundation/compat_fs_internal.h"
1717
#include "../src/foundation/win_utf8.h"
18-
#include <wchar.h>
18+
#include <winsock2.h> /* #798 follow-up: listening-socket isolation guard */
19+
#include <windows.h>
20+
#include <stdint.h>
1921
#include <stdlib.h>
22+
#include <wchar.h>
2023
#endif
2124

2225
#include <string.h>
@@ -558,6 +561,60 @@ TEST(popen_isolated_propagates_exit_code) {
558561
PASS();
559562
}
560563

564+
/* #798 follow-up (the full-repro gap flagged above): prove the EXACT handle class
565+
* that deadlocked git — an inheritable AFD/listening-socket handle, the kind the
566+
* UI HTTP server holds — does NOT cross into the cbm_popen child. Unlike the
567+
* git-version round-trip, this is deterministic on ANY Windows and does not depend
568+
* on the MSYS2 git build reproducing the NtQueryObject hang.
569+
*
570+
* We open a real listening socket, mark it inheritable, then spawn THIS test
571+
* binary through cbm_popen (a cmd.exe grandchild — exactly git's spawn shape) in
572+
* `__cbm_sockprobe` mode, passing the socket's numeric handle value. The child
573+
* reports via exit code whether that handle is a live socket in its address space:
574+
* - isolated spawn (the fix): cmd.exe inherits only {pipe, NUL}, the socket is
575+
* absent, getsockopt fails → child exit 0 → GREEN.
576+
* - raw _popen (regression): bInheritHandles=TRUE leaks the socket transitively
577+
* through cmd.exe into the child, getsockopt succeeds → child exit 42 → RED.
578+
* Verified RED with a local _popen revert, GREEN with the isolated spawn. */
579+
TEST(popen_isolates_listening_socket) {
580+
WSADATA wsa;
581+
ASSERT_EQ(WSAStartup(MAKEWORD(2, 2), &wsa), 0);
582+
583+
SOCKET ls = socket(AF_INET, SOCK_STREAM, 0);
584+
ASSERT(ls != INVALID_SOCKET);
585+
struct sockaddr_in addr;
586+
memset(&addr, 0, sizeof(addr));
587+
addr.sin_family = AF_INET;
588+
addr.sin_addr.s_addr = htonl(0x7F000001); /* 127.0.0.1 */
589+
addr.sin_port = 0; /* ephemeral */
590+
ASSERT_EQ(bind(ls, (struct sockaddr *)&addr, sizeof(addr)), 0);
591+
ASSERT_EQ(listen(ls, 1), 0);
592+
/* Winsock sockets are inheritable by default; make it explicit so a _popen
593+
* regression is guaranteed to leak it (and this test to go RED). */
594+
ASSERT(SetHandleInformation((HANDLE)ls, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
595+
596+
char self[MAX_PATH];
597+
ASSERT(GetModuleFileNameA(NULL, self, sizeof(self)) > 0);
598+
599+
char cmd[MAX_PATH + 64];
600+
snprintf(cmd, sizeof(cmd), "\"%s\" __cbm_sockprobe %llu", self,
601+
(unsigned long long)(uintptr_t)ls);
602+
603+
FILE *fp = cbm_popen(cmd, "r");
604+
ASSERT_NOT_NULL(fp);
605+
ASSERT_EQ(cbm_popen_last_was_isolated(), 1);
606+
char drain[128];
607+
while (fgets(drain, sizeof(drain), fp)) {
608+
/* the probe writes nothing to stdout, but drain to a clean EOF */
609+
}
610+
int rc = cbm_pclose(fp);
611+
closesocket(ls);
612+
WSACleanup();
613+
614+
ASSERT_EQ(rc, 0); /* 0 = socket isolated from child; 42 = leaked (regression) */
615+
PASS();
616+
}
617+
561618
#endif /* _WIN32 */
562619

563620
/* ══════════════════════════════════════════════════════════════════
@@ -629,5 +686,6 @@ SUITE(security) {
629686
/* Isolated popen — handle-inheritance regression guard for #798 */
630687
RUN_TEST(popen_isolated_git_version_round_trip);
631688
RUN_TEST(popen_isolated_propagates_exit_code);
689+
RUN_TEST(popen_isolates_listening_socket);
632690
#endif
633691
}

0 commit comments

Comments
 (0)