From ecd64026f3f351a7fef2ae4f2f3537e39f3fdf35 Mon Sep 17 00:00:00 2001 From: Alexis Christoforides Date: Thu, 16 Aug 2018 23:11:39 +0300 Subject: [PATCH] [runtime] Synchronize calls to getprotobyname() which is thread-unsafe Fixes https://github.com/mono/mono/issues/7087 --- mono/utils/networking-posix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mono/utils/networking-posix.c b/mono/utils/networking-posix.c index 85a6eba551f2f..e54230cfb8f59 100644 --- a/mono/utils/networking-posix.c +++ b/mono/utils/networking-posix.c @@ -33,6 +33,8 @@ #include #include +static mono_mutex_t networking_mutex; + static void* get_address_from_sockaddr (struct sockaddr *sa) { @@ -132,6 +134,7 @@ mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInf static int fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val) { + mono_os_mutex_lock (&networking_mutex); if (!*cache) { struct protoent *pent; @@ -139,6 +142,7 @@ fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val) *proto = pent ? pent->p_proto : default_val; *cache = 1; } + mono_os_mutex_unlock (&networking_mutex); return *proto; } @@ -352,12 +356,12 @@ mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t bufle void mono_networking_init (void) { - //nothing really + mono_os_mutex_init (&networking_mutex); } void mono_networking_shutdown (void) { - //nothing really + mono_os_mutex_destroy (&networking_mutex); } #endif