Skip to content

Commit af2fb3c

Browse files
committed
Ensure that forks dont mess up shared context
1 parent e149dc2 commit af2fb3c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

zmq.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ static
100100
static
101101
zend_long s_ctx_socket_count = 0;
102102

103+
static
104+
int s_ctx_pid = 0;
105+
103106
#ifdef ZTS
104107
static
105108
MUTEX_T s_ctx_mutex;
@@ -109,6 +112,7 @@ int s_shared_ctx_init()
109112
{
110113
if (!s_ctx) {
111114
s_ctx_mutex = tsrm_mutex_alloc();
115+
s_ctx_pid = getpid();
112116
s_ctx = zmq_init(PHP_ZMQ_SHARED_CONTEXT_THREADS);
113117
}
114118
return (s_ctx != NULL);
@@ -131,11 +135,14 @@ void s_shared_ctx_unlock()
131135
static
132136
void s_shared_ctx_destroy()
133137
{
134-
if (s_ctx) {
135-
zmq_term(s_ctx);
138+
if (s_ctx && s_ctx_pid == getpid()) {
139+
s_shared_ctx_lock();
140+
{
141+
zmq_term(s_ctx);
142+
s_ctx = NULL;
143+
}
144+
s_shared_ctx_unlock()
136145
tsrm_mutex_free(s_ctx_mutex);
137-
138-
s_ctx = NULL;
139146
s_ctx_mutex = NULL;
140147
}
141148
}
@@ -144,6 +151,7 @@ static
144151
int s_shared_ctx_init()
145152
{
146153
s_ctx = zmq_init(PHP_ZMQ_SHARED_CONTEXT_THREADS);
154+
s_ctx_pid = getpid();
147155
return (s_ctx != NULL);
148156
}
149157

@@ -153,7 +161,9 @@ static void s_shared_ctx_unlock() {}
153161
static
154162
void s_shared_ctx_destroy()
155163
{
156-
zmq_term(s_ctx);
164+
if (s_ctx && s_ctx_pid == getpid()) {
165+
zmq_term(s_ctx);
166+
}
157167
}
158168
#endif
159169

0 commit comments

Comments
 (0)