|
16 | 16 | #include "../src/foundation/log.h" |
17 | 17 | #include "../src/foundation/platform.h" |
18 | 18 | #include "../src/cli/cli.h" |
| 19 | +#include "../src/git/git_context.h" /* #798 follow-up: live-socket git-resolve repro */ |
19 | 20 | #include "../src/ui/http_server.h" |
20 | 21 | #include "test_framework.h" |
21 | 22 | #include "test_helpers.h" |
|
34 | 35 | #ifdef _WIN32 |
35 | 36 | #include <winsock2.h> |
36 | 37 | #include <ws2tcpip.h> |
| 38 | +#include <windows.h> /* #798 follow-up: CreateThread/WaitForSingleObject watchdog */ |
37 | 39 | typedef SOCKET th_sock_t; |
38 | 40 | #define th_sock_close closesocket |
39 | 41 | #define TH_SOCK_BAD INVALID_SOCKET |
40 | 42 | #else |
41 | 43 | #include <arpa/inet.h> |
42 | 44 | #include <netinet/in.h> |
43 | 45 | #include <sys/socket.h> |
| 46 | +#include <sys/time.h> /* struct timeval for the SO_RCVTIMEO watchdog (#798 follow-up) */ |
44 | 47 | #include <unistd.h> |
45 | 48 | typedef int th_sock_t; |
46 | 49 | #define th_sock_close close |
@@ -962,6 +965,126 @@ TEST(repo_info_strips_credentials_from_remote) { |
962 | 965 | PASS(); |
963 | 966 | } |
964 | 967 |
|
| 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 | + |
965 | 1088 | /* ── Suite ────────────────────────────────────────────────────── */ |
966 | 1089 |
|
967 | 1090 | SUITE(httpd) { |
@@ -1010,4 +1133,7 @@ SUITE(httpd) { |
1010 | 1133 | RUN_TEST(ui_server_slow_request_hits_deadline); |
1011 | 1134 | RUN_TEST(ui_server_access_log_redacts_query); |
1012 | 1135 | 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); |
1013 | 1139 | } |
0 commit comments