Skip to content

Commit f3098c4

Browse files
committed
8225690: Multiple AttachListener threads can be created
Reviewed-by: clanger Backport-of: 2870c9d
1 parent 2b3a641 commit f3098c4

File tree

10 files changed

+475
-102
lines changed

10 files changed

+475
-102
lines changed

src/hotspot/os/aix/attachListener_aix.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2012, 2018 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -71,17 +71,7 @@ class AixAttachListener: AllStatic {
7171
// the file descriptor for the listening socket
7272
static int _listener;
7373

74-
static void set_path(char* path) {
75-
if (path == NULL) {
76-
_has_path = false;
77-
} else {
78-
strncpy(_path, path, UNIX_PATH_MAX);
79-
_path[UNIX_PATH_MAX-1] = '\0';
80-
_has_path = true;
81-
}
82-
}
83-
84-
static void set_listener(int s) { _listener = s; }
74+
static bool _atexit_registered;
8575

8676
// reads a request from the given connected socket
8777
static AixAttachOperation* read_request(int s);
@@ -94,6 +84,19 @@ class AixAttachListener: AllStatic {
9484
ATTACH_ERROR_BADVERSION = 101 // error codes
9585
};
9686

87+
static void set_path(char* path) {
88+
if (path == NULL) {
89+
_path[0] = '\0';
90+
_has_path = false;
91+
} else {
92+
strncpy(_path, path, UNIX_PATH_MAX);
93+
_path[UNIX_PATH_MAX-1] = '\0';
94+
_has_path = true;
95+
}
96+
}
97+
98+
static void set_listener(int s) { _listener = s; }
99+
97100
// initialize the listener, returns 0 if okay
98101
static int init();
99102

@@ -130,6 +133,7 @@ class AixAttachOperation: public AttachOperation {
130133
char AixAttachListener::_path[UNIX_PATH_MAX];
131134
bool AixAttachListener::_has_path;
132135
int AixAttachListener::_listener = -1;
136+
bool AixAttachListener::_atexit_registered = false;
133137
// Shutdown marker to prevent accept blocking during clean-up
134138
bool AixAttachListener::_shutdown = false;
135139

@@ -177,17 +181,15 @@ class ArgumentIterator : public StackObj {
177181
// should be sufficient for cleanup.
178182
extern "C" {
179183
static void listener_cleanup() {
180-
static int cleanup_done;
181-
if (!cleanup_done) {
182-
cleanup_done = 1;
183-
AixAttachListener::set_shutdown(true);
184-
int s = AixAttachListener::listener();
185-
if (s != -1) {
186-
::shutdown(s, 2);
187-
}
188-
if (AixAttachListener::has_path()) {
189-
::unlink(AixAttachListener::path());
190-
}
184+
AixAttachListener::set_shutdown(true);
185+
int s = AixAttachListener::listener();
186+
if (s != -1) {
187+
AixAttachListener::set_listener(-1);
188+
::shutdown(s, 2);
189+
}
190+
if (AixAttachListener::has_path()) {
191+
::unlink(AixAttachListener::path());
192+
AixAttachListener::set_path(NULL);
191193
}
192194
}
193195
}
@@ -200,7 +202,10 @@ int AixAttachListener::init() {
200202
int listener; // listener socket (file descriptor)
201203

202204
// register function to cleanup
203-
::atexit(listener_cleanup);
205+
if (!_atexit_registered) {
206+
_atexit_registered = true;
207+
::atexit(listener_cleanup);
208+
}
204209

205210
int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
206211
os::get_temp_directory(), os::current_process_id());
@@ -515,6 +520,26 @@ int AttachListener::pd_init() {
515520
return ret_code;
516521
}
517522

523+
bool AttachListener::check_socket_file() {
524+
int ret;
525+
struct stat64 st;
526+
ret = stat64(AixAttachListener::path(), &st);
527+
if (ret == -1) { // need to restart attach listener.
528+
log_debug(attach)("Socket file %s does not exist - Restart Attach Listener",
529+
AixAttachListener::path());
530+
531+
listener_cleanup();
532+
533+
// wait to terminate current attach listener instance...
534+
while (AttachListener::transit_state(AL_INITIALIZING,
535+
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
536+
os::naked_yield();
537+
}
538+
return is_init_trigger();
539+
}
540+
return false;
541+
}
542+
518543
// Attach Listener is started lazily except in the case when
519544
// +ReduseSignalUsage is used
520545
bool AttachListener::init_at_startup() {

src/hotspot/os/bsd/attachListener_bsd.cpp

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -68,17 +68,7 @@ class BsdAttachListener: AllStatic {
6868
// the file descriptor for the listening socket
6969
static int _listener;
7070

71-
static void set_path(char* path) {
72-
if (path == NULL) {
73-
_has_path = false;
74-
} else {
75-
strncpy(_path, path, UNIX_PATH_MAX);
76-
_path[UNIX_PATH_MAX-1] = '\0';
77-
_has_path = true;
78-
}
79-
}
80-
81-
static void set_listener(int s) { _listener = s; }
71+
static bool _atexit_registered;
8272

8373
// reads a request from the given connected socket
8474
static BsdAttachOperation* read_request(int s);
@@ -91,6 +81,19 @@ class BsdAttachListener: AllStatic {
9181
ATTACH_ERROR_BADVERSION = 101 // error codes
9282
};
9383

84+
static void set_path(char* path) {
85+
if (path == NULL) {
86+
_path[0] = '\0';
87+
_has_path = false;
88+
} else {
89+
strncpy(_path, path, UNIX_PATH_MAX);
90+
_path[UNIX_PATH_MAX-1] = '\0';
91+
_has_path = true;
92+
}
93+
}
94+
95+
static void set_listener(int s) { _listener = s; }
96+
9497
// initialize the listener, returns 0 if okay
9598
static int init();
9699

@@ -124,6 +127,7 @@ class BsdAttachOperation: public AttachOperation {
124127
char BsdAttachListener::_path[UNIX_PATH_MAX];
125128
bool BsdAttachListener::_has_path;
126129
int BsdAttachListener::_listener = -1;
130+
bool BsdAttachListener::_atexit_registered = false;
127131

128132
// Supporting class to help split a buffer into individual components
129133
class ArgumentIterator : public StackObj {
@@ -158,16 +162,15 @@ class ArgumentIterator : public StackObj {
158162
// bound too.
159163
extern "C" {
160164
static void listener_cleanup() {
161-
static int cleanup_done;
162-
if (!cleanup_done) {
163-
cleanup_done = 1;
164-
int s = BsdAttachListener::listener();
165-
if (s != -1) {
166-
::close(s);
167-
}
168-
if (BsdAttachListener::has_path()) {
169-
::unlink(BsdAttachListener::path());
170-
}
165+
int s = BsdAttachListener::listener();
166+
if (s != -1) {
167+
BsdAttachListener::set_listener(-1);
168+
::shutdown(s, SHUT_RDWR);
169+
::close(s);
170+
}
171+
if (BsdAttachListener::has_path()) {
172+
::unlink(BsdAttachListener::path());
173+
BsdAttachListener::set_path(NULL);
171174
}
172175
}
173176
}
@@ -180,7 +183,10 @@ int BsdAttachListener::init() {
180183
int listener; // listener socket (file descriptor)
181184

182185
// register function to cleanup
183-
::atexit(listener_cleanup);
186+
if (!_atexit_registered) {
187+
_atexit_registered = true;
188+
::atexit(listener_cleanup);
189+
}
184190

185191
int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
186192
os::get_temp_directory(), os::current_process_id());
@@ -485,6 +491,28 @@ int AttachListener::pd_init() {
485491
return ret_code;
486492
}
487493

494+
bool AttachListener::check_socket_file() {
495+
int ret;
496+
struct stat st;
497+
ret = stat(BsdAttachListener::path(), &st);
498+
if (ret == -1) { // need to restart attach listener.
499+
log_debug(attach)("Socket file %s does not exist - Restart Attach Listener",
500+
BsdAttachListener::path());
501+
502+
listener_cleanup();
503+
504+
// wait to terminate current attach listener instance...
505+
506+
while (AttachListener::transit_state(AL_INITIALIZING,
507+
508+
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
509+
os::naked_yield();
510+
}
511+
return is_init_trigger();
512+
}
513+
return false;
514+
}
515+
488516
// Attach Listener is started lazily except in the case when
489517
// +ReduseSignalUsage is used
490518
bool AttachListener::init_at_startup() {

src/hotspot/os/linux/attachListener_linux.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,17 +69,7 @@ class LinuxAttachListener: AllStatic {
6969
// the file descriptor for the listening socket
7070
static int _listener;
7171

72-
static void set_path(char* path) {
73-
if (path == NULL) {
74-
_has_path = false;
75-
} else {
76-
strncpy(_path, path, UNIX_PATH_MAX);
77-
_path[UNIX_PATH_MAX-1] = '\0';
78-
_has_path = true;
79-
}
80-
}
81-
82-
static void set_listener(int s) { _listener = s; }
72+
static bool _atexit_registered;
8373

8474
// reads a request from the given connected socket
8575
static LinuxAttachOperation* read_request(int s);
@@ -92,6 +82,19 @@ class LinuxAttachListener: AllStatic {
9282
ATTACH_ERROR_BADVERSION = 101 // error codes
9383
};
9484

85+
static void set_path(char* path) {
86+
if (path == NULL) {
87+
_path[0] = '\0';
88+
_has_path = false;
89+
} else {
90+
strncpy(_path, path, UNIX_PATH_MAX);
91+
_path[UNIX_PATH_MAX-1] = '\0';
92+
_has_path = true;
93+
}
94+
}
95+
96+
static void set_listener(int s) { _listener = s; }
97+
9598
// initialize the listener, returns 0 if okay
9699
static int init();
97100

@@ -125,6 +128,7 @@ class LinuxAttachOperation: public AttachOperation {
125128
char LinuxAttachListener::_path[UNIX_PATH_MAX];
126129
bool LinuxAttachListener::_has_path;
127130
int LinuxAttachListener::_listener = -1;
131+
bool LinuxAttachListener::_atexit_registered = false;
128132

129133
// Supporting class to help split a buffer into individual components
130134
class ArgumentIterator : public StackObj {
@@ -159,16 +163,15 @@ class ArgumentIterator : public StackObj {
159163
// bound too.
160164
extern "C" {
161165
static void listener_cleanup() {
162-
static int cleanup_done;
163-
if (!cleanup_done) {
164-
cleanup_done = 1;
165-
int s = LinuxAttachListener::listener();
166-
if (s != -1) {
167-
::close(s);
168-
}
169-
if (LinuxAttachListener::has_path()) {
170-
::unlink(LinuxAttachListener::path());
171-
}
166+
int s = LinuxAttachListener::listener();
167+
if (s != -1) {
168+
LinuxAttachListener::set_listener(-1);
169+
::shutdown(s, SHUT_RDWR);
170+
::close(s);
171+
}
172+
if (LinuxAttachListener::has_path()) {
173+
::unlink(LinuxAttachListener::path());
174+
LinuxAttachListener::set_path(NULL);
172175
}
173176
}
174177
}
@@ -181,7 +184,10 @@ int LinuxAttachListener::init() {
181184
int listener; // listener socket (file descriptor)
182185

183186
// register function to cleanup
184-
::atexit(listener_cleanup);
187+
if (!_atexit_registered) {
188+
_atexit_registered = true;
189+
::atexit(listener_cleanup);
190+
}
185191

186192
int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
187193
os::get_temp_directory(), os::current_process_id());
@@ -485,6 +491,26 @@ int AttachListener::pd_init() {
485491
return ret_code;
486492
}
487493

494+
bool AttachListener::check_socket_file() {
495+
int ret;
496+
struct stat64 st;
497+
ret = stat64(LinuxAttachListener::path(), &st);
498+
if (ret == -1) { // need to restart attach listener.
499+
log_debug(attach)("Socket file %s does not exist - Restart Attach Listener",
500+
LinuxAttachListener::path());
501+
502+
listener_cleanup();
503+
504+
// wait to terminate current attach listener instance...
505+
while (AttachListener::transit_state(AL_INITIALIZING,
506+
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
507+
os::naked_yield();
508+
}
509+
return is_init_trigger();
510+
}
511+
return false;
512+
}
513+
488514
// Attach Listener is started lazily except in the case when
489515
// +ReduseSignalUsage is used
490516
bool AttachListener::init_at_startup() {

0 commit comments

Comments
 (0)