Discussion:
What is the right way to close TCP connections?
Egor Egorov
2013-10-10 21:03:45 UTC
Permalink
Hello.

Suppose I did uv_write() and them immediately uv_close(). Will libuv wait
for all written data to be actually sent? For how long?

In general, what is the best way to close tcp connection given that I don't
want to read anything from it anymore but I want to make sure that's it's
properly written?
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Saúl Ibarra Corretgé
2013-10-10 21:16:33 UTC
Permalink
Hi,
Post by Egor Egorov
Hello.
Suppose I did uv_write() and them immediately uv_close(). Will libuv
wait for all written data to be actually sent? For how long?
No. All pending requests will be cancelled and the write callback will
be called with UV_ECANCELLED status code.
Post by Egor Egorov
In general, what is the best way to close tcp connection given that I
don't want to read anything from it anymore but I want to make sure
that's it's properly written?
Call uv_shutdown and on the shutdown callback uv_close the handle.


Cheers,
--
Saúl Ibarra Corretgé
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Egor Egorov
2013-10-10 21:29:13 UTC
Permalink
So I was right implementing uv_shutdown(). :) Thanks.

Does this look right?


void onConnectionShutdown(uv_shutdown_t* req, int status) {
if (!uv_is_closing((uv_handle_t*) req->handle)) {
uv_close((uv_handle_t*) req->handle, (uv_close_cb) free);
}
free(req);
}

...

size_t _write_queue_size = ((uv_stream_t *)connection)->write_queue_size;

if (uv_is_writable((uv_stream_t *)connection) && _write_queue_size > 0) {
uv_shutdown_t *shutdownReq = malloc(sizeof(uv_shutdown_t));
uv_shutdown(shutdownReq, (uv_stream_t *) connection, onConnectionShutdown);
} else {
uv_close((uv_handle_t*) connection, (uv_close_cb) free);
}
Post by Saúl Ibarra Corretgé
Hi,
Post by Egor Egorov
Hello.
Suppose I did uv_write() and them immediately uv_close(). Will libuv
wait for all written data to be actually sent? For how long?
No. All pending requests will be cancelled and the write callback will
be called with UV_ECANCELLED status code.
Post by Egor Egorov
In general, what is the best way to close tcp connection given that I
don't want to read anything from it anymore but I want to make sure
that's it's properly written?
Call uv_shutdown and on the shutdown callback uv_close the handle.
Cheers,
--
Saᅵl Ibarra Corretgᅵ
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Saúl Ibarra Corretgé
2013-10-10 22:46:23 UTC
Permalink
Post by Egor Egorov
So I was right implementing uv_shutdown(). :) Thanks.
Does this look right?
void onConnectionShutdown(uv_shutdown_t* req, int status) {
if (!uv_is_closing((uv_handle_t*) req->handle)) {
Hum, so chances are you call close before shutdown actually happens?
Either way I'd just call uv_close with free as the callback in case it
was not closing already when the shutdown callback fires. If it was
already closing, don't do anything, the close callback will eventually fire.
Post by Egor Egorov
uv_close((uv_handle_t*) req->handle, (uv_close_cb) free);
}
free(req);
}
...
size_t _write_queue_size = ((uv_stream_t *)connection)->write_queue_size;
if (uv_is_writable((uv_stream_t *)connection) && _write_queue_size > 0) {
uv_shutdown_t *shutdownReq = malloc(sizeof(uv_shutdown_t));
uv_shutdown(shutdownReq, (uv_stream_t *) connection, onConnectionShutdown);
} else {
uv_close((uv_handle_t*) connection, (uv_close_cb) free);
}
Hi,
Post by Egor Egorov
Hello.
Suppose I did uv_write() and them immediately uv_close(). Will libuv
wait for all written data to be actually sent? For how long?
No. All pending requests will be cancelled and the write callback will
be called with UV_ECANCELLED status code.
Post by Egor Egorov
In general, what is the best way to close tcp connection given
that I
Post by Egor Egorov
don't want to read anything from it anymore but I want to make sure
that's it's properly written?
Call uv_shutdown and on the shutdown callback uv_close the handle.
Cheers,
--
Sa�l Ibarra Corretg�
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
--
Saúl Ibarra Corretgé
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Egor Egorov
2013-10-10 22:48:48 UTC
Permalink
yeah, this check in onShutdown callback is clearly redundant; perhaps it
was put there "just in case". Otherwise the code is correct?
Post by Saúl Ibarra Corretgé
Post by Egor Egorov
So I was right implementing uv_shutdown(). :) Thanks.
Does this look right?
void onConnectionShutdown(uv_shutdown_t* req, int status) {
if (!uv_is_closing((uv_handle_t*) req->handle)) {
Hum, so chances are you call close before shutdown actually happens?
Either way I'd just call uv_close with free as the callback in case it
was not closing already when the shutdown callback fires. If it was
already closing, don't do anything, the close callback will eventually fire.
Post by Egor Egorov
uv_close((uv_handle_t*) req->handle, (uv_close_cb) free);
}
free(req);
}
...
size_t _write_queue_size = ((uv_stream_t
*)connection)->write_queue_size;
Post by Egor Egorov
if (uv_is_writable((uv_stream_t *)connection) && _write_queue_size > 0)
{
Post by Egor Egorov
uv_shutdown_t *shutdownReq = malloc(sizeof(uv_shutdown_t));
uv_shutdown(shutdownReq, (uv_stream_t *) connection,
onConnectionShutdown);
Post by Egor Egorov
} else {
uv_close((uv_handle_t*) connection, (uv_close_cb) free);
}
On Friday, October 11, 2013 12:16:33 AM UTC+3, Saᅵl Ibarra Corretgᅵ
Hi,
Post by Egor Egorov
Hello.
Suppose I did uv_write() and them immediately uv_close(). Will
libuv
Post by Egor Egorov
Post by Egor Egorov
wait for all written data to be actually sent? For how long?
No. All pending requests will be cancelled and the write callback
will
Post by Egor Egorov
be called with UV_ECANCELLED status code.
Post by Egor Egorov
In general, what is the best way to close tcp connection given
that I
Post by Egor Egorov
don't want to read anything from it anymore but I want to make
sure
Post by Egor Egorov
Post by Egor Egorov
that's it's properly written?
Call uv_shutdown and on the shutdown callback uv_close the handle.
Cheers,
--
Saᅵl Ibarra Corretgᅵ
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google
Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
--
Saᅵl Ibarra Corretgᅵ
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Saúl Ibarra Corretgé
2013-10-10 22:58:46 UTC
Permalink
Post by Egor Egorov
yeah, this check in onShutdown callback is clearly redundant; perhaps
it was put there "just in case". Otherwise the code is correct?
Other than that, looks good to me. Just be careful, because if that if
passes, you'd call free, but that doesn't mean that the close callback
was called, so Bad Things (TM) will happen.
Post by Egor Egorov
Post by Egor Egorov
So I was right implementing uv_shutdown(). :) Thanks.
Does this look right?
void onConnectionShutdown(uv_shutdown_t* req, int status) {
if (!uv_is_closing((uv_handle_t*) req->handle)) {
Hum, so chances are you call close before shutdown actually happens?
Either way I'd just call uv_close with free as the callback in case it
was not closing already when the shutdown callback fires. If it was
already closing, don't do anything, the close callback will eventually fire.
Post by Egor Egorov
uv_close((uv_handle_t*) req->handle, (uv_close_cb) free);
}
free(req);
}
...
size_t _write_queue_size = ((uv_stream_t
*)connection)->write_queue_size;
Post by Egor Egorov
if (uv_is_writable((uv_stream_t *)connection) &&
_write_queue_size > 0) {
Post by Egor Egorov
uv_shutdown_t *shutdownReq = malloc(sizeof(uv_shutdown_t));
uv_shutdown(shutdownReq, (uv_stream_t *) connection,
onConnectionShutdown);
Post by Egor Egorov
} else {
uv_close((uv_handle_t*) connection, (uv_close_cb) free);
}
On Friday, October 11, 2013 12:16:33 AM UTC+3, Sa�l Ibarra
Hi,
Post by Egor Egorov
Hello.
Suppose I did uv_write() and them immediately uv_close().
Will libuv
Post by Egor Egorov
Post by Egor Egorov
wait for all written data to be actually sent? For how long?
No. All pending requests will be cancelled and the write
callback will
Post by Egor Egorov
be called with UV_ECANCELLED status code.
Post by Egor Egorov
In general, what is the best way to close tcp connection
given
Post by Egor Egorov
that I
Post by Egor Egorov
don't want to read anything from it anymore but I want to
make sure
Post by Egor Egorov
Post by Egor Egorov
that's it's properly written?
Call uv_shutdown and on the shutdown callback uv_close the
handle.
Post by Egor Egorov
Cheers,
--
Sa�l Ibarra Corretg�
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google
Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it,
send
<javascript:>.
Post by Egor Egorov
Visit this group at http://groups.google.com/group/libuv
<http://groups.google.com/group/libuv>.
Post by Egor Egorov
For more options, visit https://groups.google.com/groups/opt_out
<https://groups.google.com/groups/opt_out>.
--
Sa�l Ibarra Corretg�
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
--
Saúl Ibarra Corretgé
http://bettercallsaghul.com
--
You received this message because you are subscribed to the Google Groups "libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libuv+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...