From be0e1e144bbf5c08d8ac4f2a4347ca4e16dbc1c2 Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Wed, 5 Apr 2017 10:42:58 -0600 Subject: [PATCH] FS-10167: Fixed some issues that appeared primarily under windows testing, committing to determine if a specific read access violation occurs under linux as well. --- libs/libblade/src/blade_module_wss.c | 6 +- libs/libblade/src/blade_protocol.c | 4 +- libs/libblade/src/blade_stack.c | 15 +- libs/libks/libks.sln | 10 ++ libs/libks/src/ks_pool.c | 1 + libs/libks/test/testpolling.c | 115 ++++++++++++++++ libs/libks/test/testpolling.vcxproj | 198 +++++++++++++++++++++++++++ 7 files changed, 336 insertions(+), 13 deletions(-) create mode 100644 libs/libks/test/testpolling.c create mode 100644 libs/libks/test/testpolling.vcxproj diff --git a/libs/libblade/src/blade_module_wss.c b/libs/libblade/src/blade_module_wss.c index c270321cb3..aed49c9ce3 100644 --- a/libs/libblade/src/blade_module_wss.c +++ b/libs/libblade/src/blade_module_wss.c @@ -523,7 +523,7 @@ ks_status_t blade_module_wss_listen(blade_module_wss_t *bm_wss, ks_sockaddr_t *a sizeof(struct pollfd) * bm_wss->listeners_count); ks_assert(bm_wss->listeners_poll); bm_wss->listeners_poll[listener_index].fd = listener; - bm_wss->listeners_poll[listener_index].events = POLLIN | POLLERR; + bm_wss->listeners_poll[listener_index].events = POLLIN; // | POLLERR; ks_log(KS_LOG_DEBUG, "Bound %s on port %d at index %d\n", ks_addr_get_host(addr), ks_addr_get_port(addr), listener_index); @@ -784,7 +784,7 @@ ks_status_t blade_transport_wss_on_send(blade_connection_t *bc, cJSON *json) ks_status_t blade_transport_wss_read(blade_transport_wss_t *bt_wss, cJSON **json) { // @todo get exact timeout from service config? - int32_t poll_flags = ks_wait_sock(bt_wss->sock, 100, KS_POLL_READ | KS_POLL_ERROR); + int32_t poll_flags = ks_wait_sock(bt_wss->sock, 100, KS_POLL_READ); // | KS_POLL_ERROR); *json = NULL; @@ -836,7 +836,7 @@ ks_status_t blade_transport_wss_rpc_error_send(blade_connection_t *bc, const cha cJSON *json = NULL; ks_assert(bc); - ks_assert(id); + //ks_assert(id); ks_assert(message); bt_wss = (blade_transport_wss_t *)blade_connection_transport_get(bc); diff --git a/libs/libblade/src/blade_protocol.c b/libs/libblade/src/blade_protocol.c index 2752ce9fff..7a120a8d65 100644 --- a/libs/libblade/src/blade_protocol.c +++ b/libs/libblade/src/blade_protocol.c @@ -236,14 +236,14 @@ KS_DECLARE(ks_status_t) blade_rpc_error_create(ks_pool_t *pool, cJSON **json, cJ ks_assert(pool); ks_assert(json); - ks_assert(id); + //ks_assert(id); ks_assert(message); root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "jsonrpc", "2.0"); - cJSON_AddStringToObject(root, "id", id); + if (id) cJSON_AddStringToObject(root, "id", id); e = cJSON_CreateObject(); cJSON_AddNumberToObject(e, "code", code); diff --git a/libs/libblade/src/blade_stack.c b/libs/libblade/src/blade_stack.c index 5a8a81b800..a66d1f58ec 100644 --- a/libs/libblade/src/blade_stack.c +++ b/libs/libblade/src/blade_stack.c @@ -324,13 +324,6 @@ KS_DECLARE(ks_status_t) blade_handle_shutdown(blade_handle_t *bh) ks_assert(bh); - if (bh->worker_thread) { - bh->shutdown = KS_TRUE; - ks_thread_join(bh->worker_thread); - ks_pool_free(bh->pool, &bh->worker_thread); - bh->shutdown = KS_FALSE; - } - while ((it = ks_hash_first(bh->requests, KS_UNLOCKED))) { void *key = NULL; blade_request_t *value = NULL; @@ -346,7 +339,6 @@ KS_DECLARE(ks_status_t) blade_handle_shutdown(blade_handle_t *bh) blade_session_t *value = NULL; ks_hash_this(it, (const void **)&key, NULL, (void **)&value); - //ks_hash_remove(bh->sessions, key); blade_session_hangup(value); } @@ -375,6 +367,13 @@ KS_DECLARE(ks_status_t) blade_handle_shutdown(blade_handle_t *bh) if (blade_handle_datastore_available(bh)) blade_datastore_destroy(&bh->datastore); + if (bh->worker_thread) { + bh->shutdown = KS_TRUE; + ks_thread_join(bh->worker_thread); + ks_pool_free(bh->pool, &bh->worker_thread); + bh->shutdown = KS_FALSE; + } + return KS_STATUS_SUCCESS; } diff --git a/libs/libks/libks.sln b/libs/libks/libks.sln index 0ff2f7012a..bafb87e2bb 100644 --- a/libs/libks/libks.sln +++ b/libs/libks/libks.sln @@ -31,6 +31,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ssleay32", "..\win32\openss EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testrealloc", "test\testrealloc.vcxproj", "{22BCE97F-2477-427D-83FE-74851DDBC57E}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testpolling", "test\testpolling.vcxproj", "{699A44BF-D03D-469F-83B2-C52C0B4B95BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -151,6 +153,14 @@ Global {22BCE97F-2477-427D-83FE-74851DDBC57E}.Release|x64.Build.0 = Release|x64 {22BCE97F-2477-427D-83FE-74851DDBC57E}.Release|x86.ActiveCfg = Release|Win32 {22BCE97F-2477-427D-83FE-74851DDBC57E}.Release|x86.Build.0 = Release|Win32 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Debug|x64.ActiveCfg = Debug|x64 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Debug|x64.Build.0 = Debug|x64 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Debug|x86.ActiveCfg = Debug|Win32 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Debug|x86.Build.0 = Debug|Win32 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Release|x64.ActiveCfg = Release|x64 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Release|x64.Build.0 = Release|x64 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Release|x86.ActiveCfg = Release|Win32 + {699A44BF-D03D-469F-83B2-C52C0B4B95BD}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/libs/libks/src/ks_pool.c b/libs/libks/src/ks_pool.c index 2b0b12d112..0bafffe3d9 100644 --- a/libs/libks/src/ks_pool.c +++ b/libs/libks/src/ks_pool.c @@ -1285,6 +1285,7 @@ static ks_status_t ks_pool_raw_close(ks_pool_t *mp_p) block_p->mb_magic2 = 0; /* record the next pointer because it might be invalidated below */ next_p = block_p->mb_next_p; + ret = free_pages(block_p, (unsigned long)((char *) block_p->mb_bounds_p - (char *) block_p)); if (ret != KS_STATUS_SUCCESS) { diff --git a/libs/libks/test/testpolling.c b/libs/libks/test/testpolling.c new file mode 100644 index 0000000000..c7f08b5ef1 --- /dev/null +++ b/libs/libks/test/testpolling.c @@ -0,0 +1,115 @@ +#include "ks.h" + +#include +#include +#include +#include "tap.h" + +ks_socket_t start_listen(ks_sockaddr_t *addr) +{ + ks_socket_t listener = KS_SOCK_INVALID; + ks_status_t ret = KS_STATUS_SUCCESS; + + ks_assert(addr); + + if ((listener = socket(addr->family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) { + ks_log(KS_LOG_DEBUG, "listener == KS_SOCK_INVALID\n"); + ret = KS_STATUS_FAIL; + goto done; + } + + ks_socket_option(listener, SO_REUSEADDR, KS_TRUE); + ks_socket_option(listener, TCP_NODELAY, KS_TRUE); + if (addr->family == AF_INET6) ks_socket_option(listener, IPV6_V6ONLY, KS_TRUE); + + if (ks_addr_bind(listener, addr) != KS_STATUS_SUCCESS) { + ks_log(KS_LOG_DEBUG, "ks_addr_bind(listener, addr) != KS_STATUS_SUCCESS\n"); + ret = KS_STATUS_FAIL; + goto done; + } + + if (listen(listener, 4) != 0) { + ks_log(KS_LOG_DEBUG, "listen(listener, backlog) != 0\n"); + ret = KS_STATUS_FAIL; + goto done; + } + +done: + if (ret != KS_STATUS_SUCCESS) { + if (listener != KS_SOCK_INVALID) { + ks_socket_shutdown(listener, SHUT_RDWR); + ks_socket_close(&listener); + listener = KS_SOCK_INVALID; + } + } + return listener; +} + +int main(int argc, char **argv) +{ + ks_pool_t *pool = NULL; + struct pollfd *listeners_poll = NULL; + int32_t listeners_count = 0; + int32_t listener_index = -1; + ks_sockaddr_t addr; + ks_socket_t listener = KS_SOCK_INVALID; + ks_socket_t sock = KS_SOCK_INVALID; + + ks_init(); + + plan(2); + + ks_pool_open(&pool); + + ks_addr_set(&addr, "0.0.0.0", 1234, AF_INET); + + listener = start_listen(&addr); + listener_index = listeners_count++; + listeners_poll = (struct pollfd *)ks_pool_resize(pool, listeners_poll, sizeof(struct pollfd) * listeners_count); + ok(listeners_poll != NULL); + + listeners_poll[listener_index].fd = listener; + listeners_poll[listener_index].events = POLLIN; + + while (1) { + int p = ks_poll(listeners_poll, listeners_count, 100); + if (p > 0) { + printf("POLL event occurred\n"); + for (int32_t index = 0; index < listeners_count; ++index) { + if (listeners_poll[index].revents & POLLERR) { + printf("POLLERR on index %d\n", index); + break; + } + if (!(listeners_poll[index].revents & POLLIN)) continue; + + printf("POLLIN on index %d\n", index); + + if ((sock = accept(listeners_poll[index].fd, NULL, NULL)) == KS_SOCK_INVALID) { + printf("Accept failed on index %d\n", index); + continue; + } + + printf("Accept success on index %d\n", index); + } + break; + } else if (p < 0) { + printf("Polling socket error %d\n", WSAGetLastError()); + } + } + + ok(sock != KS_SOCK_INVALID); + + if (sock != KS_SOCK_INVALID) ks_socket_close(&sock); + + for (int index = 0; index < listeners_count; ++index) { + listener = listeners_poll[index].fd; + ks_socket_close(&listener); + } + ks_pool_free(pool, &listeners_poll); + + ks_pool_close(&pool); + + ks_shutdown(); + + done_testing(); +} \ No newline at end of file diff --git a/libs/libks/test/testpolling.vcxproj b/libs/libks/test/testpolling.vcxproj new file mode 100644 index 0000000000..c863af6343 --- /dev/null +++ b/libs/libks/test/testpolling.vcxproj @@ -0,0 +1,198 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {699A44BF-D03D-469F-83B2-C52C0B4B95BD} + Win32Proj + testpolling + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(Platform)\$(Configuration)\$(ProjectName)\ + $(SolutionDir)$(Platform)\$(Configuration)\ + $(SolutionDir);$(SolutionDir)\crypt;$(SolutionDir)\openssl\include;$(IncludePath) + $(LibraryPath) + + + true + $(Platform)\$(Configuration)\$(ProjectName)\ + $(SolutionDir);$(SolutionDir)\crypt;$(SolutionDir)\openssl\include64;$(IncludePath) + $(LibraryPath) + + + false + $(Platform)\$(Configuration)\$(ProjectName)\ + $(SolutionDir)$(Platform)\$(Configuration)\ + $(SolutionDir);$(SolutionDir)\crypt;$(SolutionDir)\openssl\include;$(IncludePath) + $(LibraryPath) + + + false + $(Platform)\$(Configuration)\$(ProjectName)\ + $(SolutionDir);$(SolutionDir)\crypt;$(SolutionDir)\openssl\include64;$(IncludePath) + $(LibraryPath) + + + + + + Level3 + Disabled + _CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)..\win32\openssl\include;$(SolutionDir)..\win32\openssl\include_x86;../src/include;. + 4090 + true + false + + + Console + true + %(AdditionalDependencies) + + + + + + + Level3 + Disabled + _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)..\win32\openssl\include;$(SolutionDir)..\win32\openssl\include_x64;../src/include;. + 4090 + true + false + + + Console + true + %(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + _CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)..\win32\openssl\include;$(SolutionDir)..\win32\openssl\include_x86;../src/include;. + 4090 + true + + + Console + true + true + true + %(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)..\win32\openssl\include;$(SolutionDir)..\win32\openssl\include_x64;../src/include;. + 4090 + true + + + Console + true + true + true + %(AdditionalDependencies) + + + + + {d331904d-a00a-4694-a5a3-fcff64ab5dbe} + + + {b4b62169-5ad4-4559-8707-3d933ac5db39} + + + {70d178d8-1100-4152-86c0-809a91cff832} + + + + + + + + + + \ No newline at end of file