0004-BUG-MAJOR-lua-socket-resources-not-detroyed-when-the.patch 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. From 2823f54f706f56304970313cb14a98a4ce20d5ab Mon Sep 17 00:00:00 2001
  2. From: Thierry FOURNIER <thierry.fournier@ozon.io>
  3. Date: Sun, 16 Jul 2017 20:48:54 +0200
  4. Subject: [PATCH 04/18] BUG/MAJOR: lua/socket: resources not detroyed when the
  5. socket is aborted
  6. In some cases, the socket is misused. The user can open socket and never
  7. close it, or open the socket and close it without sending data. This
  8. causes resources leak on all resources associated to the stream (buffer,
  9. spoe, ...)
  10. This is caused by the stream_shutdown function which is called outside
  11. of the stream execution process. Sometimes, the shtudown is required
  12. while the stream is not started, so the cleanup is ignored.
  13. This patch change the shutdown mode of the session. Now if the session is
  14. no longer used and the Lua want to destroy it, it just set a destroy flag
  15. and the session kill itself.
  16. This patch should be backported in 1.6 and 1.7
  17. (cherry picked from cmomit b13b20a19aacb039a33f886e38a181b00c9a6d41)
  18. Signed-off-by: Willy Tarreau <w@1wt.eu>
  19. ---
  20. include/types/applet.h | 1 +
  21. src/hlua.c | 16 ++++++++++++++--
  22. 2 files changed, 15 insertions(+), 2 deletions(-)
  23. diff --git a/include/types/applet.h b/include/types/applet.h
  24. index 46b2bc10..aee9167e 100644
  25. --- a/include/types/applet.h
  26. +++ b/include/types/applet.h
  27. @@ -122,6 +122,7 @@ struct appctx {
  28. struct hlua_socket *socket;
  29. struct list wake_on_read;
  30. struct list wake_on_write;
  31. + int die;
  32. } hlua;
  33. struct {
  34. struct hlua hlua;
  35. diff --git a/src/hlua.c b/src/hlua.c
  36. index 2d312804..eb003558 100644
  37. --- a/src/hlua.c
  38. +++ b/src/hlua.c
  39. @@ -1544,6 +1544,15 @@ static void hlua_socket_handler(struct appctx *appctx)
  40. struct stream_interface *si = appctx->owner;
  41. struct connection *c = objt_conn(si_opposite(si)->end);
  42. + if (appctx->ctx.hlua.die) {
  43. + si_shutw(si);
  44. + si_shutr(si);
  45. + si_ic(si)->flags |= CF_READ_NULL;
  46. + hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
  47. + hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
  48. + stream_shutdown(si_strm(si), SF_ERR_KILLED);
  49. + }
  50. +
  51. /* If the connection object is not avalaible, close all the
  52. * streams and wakeup everithing waiting for.
  53. */
  54. @@ -1619,9 +1628,10 @@ __LJMP static int hlua_socket_gc(lua_State *L)
  55. /* Remove all reference between the Lua stack and the coroutine stream. */
  56. appctx = objt_appctx(socket->s->si[0].end);
  57. - stream_shutdown(socket->s, SF_ERR_KILLED);
  58. socket->s = NULL;
  59. appctx->ctx.hlua.socket = NULL;
  60. + appctx->ctx.hlua.die = 1;
  61. + appctx_wakeup(appctx);
  62. return 0;
  63. }
  64. @@ -1641,10 +1651,11 @@ __LJMP static int hlua_socket_close(lua_State *L)
  65. return 0;
  66. /* Close the stream and remove the associated stop task. */
  67. - stream_shutdown(socket->s, SF_ERR_KILLED);
  68. appctx = objt_appctx(socket->s->si[0].end);
  69. appctx->ctx.hlua.socket = NULL;
  70. socket->s = NULL;
  71. + appctx->ctx.hlua.die = 1;
  72. + appctx_wakeup(appctx);
  73. return 0;
  74. }
  75. @@ -2316,6 +2327,7 @@ __LJMP static int hlua_socket_new(lua_State *L)
  76. appctx->ctx.hlua.socket = socket;
  77. appctx->ctx.hlua.connected = 0;
  78. + appctx->ctx.hlua.die = 0;
  79. LIST_INIT(&appctx->ctx.hlua.wake_on_write);
  80. LIST_INIT(&appctx->ctx.hlua.wake_on_read);
  81. --
  82. 2.13.0