Permalink
Browse files

Revert broken test and fix context termination issues

  • Loading branch information...
1 parent a2b0693 commit ec60b1db6e8429dcd731bb025fe8d46090a7e20e @evax evax committed Apr 8, 2011
Showing with 26 additions and 4 deletions.
  1. +24 −2 c_src/erlzmq_nif.c
  2. +2 −2 test/erlzmq_test.erl
View
@@ -506,6 +506,10 @@ NIF(erlzmq_nif_send)
memcpy(zmq_msg_data(&msg), &req, sizeof(erlzmq_thread_request_t));
enif_mutex_lock(socket->context->mutex);
+ if (socket->context->thread_socket_name == NULL) {
+ enif_mutex_unlock(socket->context->mutex);
+ return return_zmq_errno(env, ETERM);
+ }
if (zmq_send(socket->context->thread_socket, &msg, 0)) {
enif_mutex_unlock(socket->context->mutex);
@@ -580,6 +584,10 @@ NIF(erlzmq_nif_recv)
memcpy(zmq_msg_data(&msg), &req, sizeof(erlzmq_thread_request_t));
enif_mutex_lock(socket->context->mutex);
+ if (socket->context->thread_socket_name == NULL) {
+ enif_mutex_unlock(socket->context->mutex);
+ return return_zmq_errno(env, ETERM);
+ }
if (zmq_send(socket->context->thread_socket, &msg, 0)) {
enif_mutex_unlock(socket->context->mutex);
@@ -636,6 +644,17 @@ NIF(erlzmq_nif_close)
memcpy(zmq_msg_data(&msg), &req, sizeof(erlzmq_thread_request_t));
enif_mutex_lock(socket->context->mutex);
+ if (socket->context->thread_socket_name == NULL) {
+ // context is gone
+ enif_mutex_lock(socket->mutex);
+ zmq_msg_close(&msg);
+ zmq_close(socket->socket_zmq);
+ enif_mutex_unlock(socket->mutex);
+ enif_mutex_destroy(socket->mutex);
+ enif_release_resource(socket);
+ enif_mutex_unlock(socket->context->mutex);
+ return enif_make_atom(env, "ok");
+ }
if (zmq_send(socket->context->thread_socket, &msg, 0)) {
enif_mutex_unlock(socket->context->mutex);
zmq_msg_close(&msg);
@@ -901,6 +920,10 @@ static void * polling_thread(void * handle)
zmq_msg_close(&msg);
}
else if (r->type == ERLZMQ_THREAD_REQUEST_TERM) {
+ enif_mutex_lock(context->mutex);
+ free(context->thread_socket_name);
+ context->thread_socket_name = NULL;
+ enif_mutex_unlock(context->mutex);
// cleanup pending requests
for (i = 1; i < vector_count(&requests); ++i) {
erlzmq_thread_request_t * r_old = vector_get(erlzmq_thread_request_t,
@@ -922,9 +945,8 @@ static void * polling_thread(void * handle)
zmq_close(thread_socket);
zmq_close(context->thread_socket);
enif_mutex_unlock(context->mutex);
- enif_mutex_destroy(context->mutex);
- free(context->thread_socket_name);
zmq_term(context->context_zmq);
+ enif_mutex_destroy(context->mutex);
enif_release_resource(context);
// notify the waiting request
enif_send(NULL, &r->data.term.pid, r->data.term.env,
View
@@ -83,10 +83,10 @@ shutdown_blocking_test() ->
shutdown_blocking_unblocking_test() ->
{ok, C} = erlzmq:context(),
{ok, S} = erlzmq:socket(C, [pub, {active, false}]),
- erlzmq:close(S),
- V = erlzmq:term(C, 0),
+ V = erlzmq:term(C, 500),
?assertMatch({error, {timeout, _}}, V),
{error, {timeout, Ref}} = V,
+ erlzmq:close(S),
receive
{Ref, ok} ->
ok

0 comments on commit ec60b1d

Please sign in to comment.