diff options
author | Matthew Jordan <mjordan@digium.com> | 2014-06-26 12:21:14 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2014-06-26 12:21:14 +0000 |
commit | 365ae7523b45f18abb1418f498561cc2c8cbf680 (patch) | |
tree | 2d1ce4e889fedf5885299baef55a16df464f7a21 /channels/chan_sip.c | |
parent | d171e0b2e96ca1cc2cf6c53cdd9d5a3c876be91b (diff) |
res_http_websocket: Close websocket correctly and use careful fwrite
When a client takes a long time to process information received from Asterisk,
a write operation using fwrite may fail to write all information. This causes
the underlying file stream to be in an unknown state, such that the socket
must be disconnected. Unfortunately, there are two problems with this in
Asterisk's existing websocket code:
1. Periodically, during the read loop, Asterisk must write to the connected
websocket to respond to pings. As such, Asterisk maintains a reference to
the session during the loop. When ast_http_websocket_write fails, it may
cause the session to decrement its ref count, but this in and of itself
does not break the read loop. The read loop's write, on the other hand,
does not break the loop if it fails. This causes the socket to get in a
'stuck' state, preventing the client from reconnecting to the server.
2. More importantly, however, is that the fwrite in ast_http_websocket_write
fails with a large volume of data when the client takes awhile to process
the information. When it does fail, it fails writing only a portion of
the bytes. With some debugging, it was shown that this was failing in a
similar fashion to ASTERISK-12767. Switching this over to ast_careful_fwrite
with a long enough timeout solved the problem.
Note that this version of the patch, unlike r417310 in Asterisk 11, exposes
configuration options beyond just chan_sip's sip.conf. Configuration options
to configure the write timeout have also been added to pjsip.conf and ari.conf.
#ASTERISK-23917 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3624/
........
Merged revisions 417310 from http://svn.asterisk.org/svn/asterisk/branches/11
........
Merged revisions 417311 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@417317 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'channels/chan_sip.c')
-rw-r--r-- | channels/chan_sip.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 5e829357f..14b460c14 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -2665,6 +2665,10 @@ static void sip_websocket_callback(struct ast_websocket *session, struct ast_var goto end; } + if (ast_websocket_set_timeout(session, sip_cfg.websocket_write_timeout)) { + goto end; + } + while ((res = ast_wait_for_input(ast_websocket_fd(session), -1)) > 0) { char *payload; uint64_t payload_len; @@ -32009,6 +32013,12 @@ static int reload_config(enum channelreloadreason reason) ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot)); } else if (!strcasecmp(v->name, "refer_addheaders")) { global_refer_addheaders = ast_true(v->value); + } else if (!strcasecmp(v->name, "websocket_write_timeout")) { + if (sscanf(v->value, "%30d", &sip_cfg.websocket_write_timeout) != 1 + || sip_cfg.websocket_write_timeout < 0) { + ast_log(LOG_WARNING, "'%s' is not a valid websocket_write_timeout value at line %d. Using default '%d'.\n", v->value, v->lineno, AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT); + sip_cfg.websocket_write_timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT; + } } } |