5

When I try to use the function uv_close((uv_handle_t*)client,NULL) in libuv library to actively close the TCP connection with the client, the error

"main: src/unix/core.c:117: uv_close: Assertion `!uv__is_closing(handle)' failed."

was reported. I search a lot online, but I still cannot find the correct way to solve the problem. I wish someone can tell me why this problem resulted and how to solve it.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Trl
  • 51
  • 1
  • 2
  • I'm not too familiar with libuv, but try checking if the handle is being closed already before calling uv_close. I think I see where the assertion is in the source code on github. – Robert Prévost Nov 28 '17 at 03:06

1 Answers1

3

You are trying to close a handle that is either already closed or in a closing state (that is, somewhere along the process that brings the handle from being alive to being closed).

As you can see from the code of libuv, the uv_close function starts as:

void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
  assert(!uv__is_closing(handle));

  handle->flags |= UV_CLOSING;

  // ...

Where uv__is_closing is defined as:

#define uv__is_closing(h)                                                     \
  (((h)->flags & (UV_CLOSING |  UV_CLOSED)) != 0)

To sum up, as soon as uv_close is invoked on a handle, the UV_CLOSING flag is set and it's checked on subsequent calls to avoid multiple runs of the close function. In other terms, you can close a handle only once.

The error comes out because you are probably invoking uv_close more than once for a handle. However, it's hard to say without looking at the real code.

As a side note, you can use uv_is_closing to test your handle if you are in doubt. It's a kind of alias for uv__is_closing.

skypjack
  • 49,335
  • 19
  • 95
  • 187