Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Petrenko <sergepetrenko@tarantool.org>
To: tarantool-patches@freelists.org
Cc: Konstantin Osipov <kostja@tarantool.org>
Subject: [tarantool-patches] Re: [PATCH] tarantoolctl: return an error on enter to a dead socket.
Date: Fri, 3 Aug 2018 11:52:40 +0300	[thread overview]
Message-ID: <E88A69A0-D00A-4C9D-AA94-7A1425C50897@tarantool.org> (raw)
In-Reply-To: <865891b0-f41e-342c-6a02-309502ff1b8a@tarantool.org>

[-- Attachment #1: Type: text/plain, Size: 8238 bytes --]



> 18 июля 2018 г., в 12:19, Sergey Petrenko <sergepetrenko@tarantool.org> написал(а):
> 
> Hi!
> 
> 18.07.2018 8:19, Konstantin Osipov пишет:
>> * Serge Petrenko <sergepetrenko@tarantool.org> [18/07/16 15:14]:
>> 
>>> Tarantoolctl enter didn't check whether connection to a socket was established
>>> if a socket file existed. It just executed a local console.
>>> Fix this by adding a check and an error, also add a test case.
>>> 
>>> Closes #3364
>>>  extra/dist/tarantoolctl.in         | 19 ++++++++++++-------
>>>  test/app-tap/tarantoolctl.test.lua | 29 ++++++++++++++++++++++++++++-
>>>  2 files changed, 40 insertions(+), 8 deletions(-)
>>> 
>>> +    local status, err = pcall(function()
>>> +        test:test("check error codes in case of enter", function(test_i)
>>> +            test_i:plan(6)
>>> +            check_ok(test_i, dir, 'enter', 'script', 1, nil, "Can't connect to")
>>> +            check_ok(test_i, dir, 'start', 'script', 0)
>>> +            os.execute(("kill $(cat %s/script.pid)"):format(dir))
>> I don't understand how this works. Simply sending
>> SIGTERM to tarantool would should it down gracefully and remove
>> the socket. If it doesn't, it's a bug. You should use SIGKILL if
> Doing
>   tarantoolctl start testctl
>   kill (tarantool instance pid)
> Leaves the socket intact on my pc.
> tarantoolctl stop also first explicitly deletes the socket
> file and then sends SIGTERM to the instance.
> Don't know if it's ok.

Hi! As we discussed verbally, tarantool shouldn’t leave a unix socket after its exit, so it’s a bug.
I fixed it and rebased the patch on top of latest 1.10
The thing is that iproto thread should unlink the file (if any) upon its stop, but we never stop
iproto thread. I tried to implement a proper stop function, like the one I wrote for replication_shutdown(),
but couldn’t make it work.
So all I have is a simple function that is executed in tx thread and 
calls evio_service_stop() on binary service, which, in turn, unlinks the socket.
I don’t know if it’s ok to call evio_service_stop() from another thread, so I need your comments.

I also added some test cases to check for correct creation and deletion of unix socket under tarantoolctl.
Please see new diff below.

>> you wanted to create a dead socket. But then again why do you need
>> to start an instance in order to get a dead socket? Can't you make
>> it simpler and create a dead socket with fio API?
> Do you mean create a file with fio.open(sock, 'O_CREAT')?
> It won't be a 'real' socket, but I tried it, and the tests seem to
> work.
> Because AFAIU there is no way to create a unix socket without
> starting to listen on it.
> That's what I'm doing now:
>         test_i:plan(4)
>         check_ok(test_i, dir, 'enter', 'script', 1, nil, "Can't connect to")
>         local console_sock = 'script.control'
>         console_sock = fio.pathjoin(fio.dirname(dir), console_sock)
>         fio.open(console_sock, 'O_CREAT')
>         check_ok(test_i, dir, 'enter', 'script', 1, nil, "Can't connect to")
>         fio.unlink(console_sock)

 extra/dist/tarantoolctl.in         | 19 +++++++++------
 src/box/box.cc                     |  2 ++
 src/box/iproto.cc                  | 16 +++++++++++++
 src/box/iproto.h                   |  3 +++
 test/app-tap/tarantoolctl.test.lua | 48 +++++++++++++++++++++++++++++++++++++-
 5 files changed, 80 insertions(+), 8 deletions(-)

diff --git a/extra/dist/tarantoolctl.in b/extra/dist/tarantoolctl.in
index f0529734f..8d52a4d9c 100755
--- a/extra/dist/tarantoolctl.in
+++ b/extra/dist/tarantoolctl.in
@@ -638,15 +638,20 @@ local function enter()
         end
         return 1
     end
-
-    local cmd = string.format(
-        "require('console').connect('%s', { connect_timeout = %s })",
-        console_sock, TIMEOUT_INFINITY
-    )
-
-    console.on_start(function(self) self:eval(cmd) end)
+    local status, ret
+    console.on_start(function(self)
+        status, ret = pcall(console.connect, console_sock,
+              {connect_timeout = TIMEOUT_INFINITY})
+        if not status then
+            log.error("Can't connect to %s (%s)", console_sock_path, ret)
+            self.running = false
+        end
+    end)
     console.on_client_disconnect(function(self) self.running = false end)
     console.start()
+    if not status then
+        return 1
+    end
     return 0
 end
 
diff --git a/src/box/box.cc b/src/box/box.cc
index ee12d5738..adc3204bf 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -1606,6 +1606,8 @@ box_free(void)
 		tuple_free();
 		port_free();
 #endif
+		/* If listening on a unix socket, unlink it. */
+		iproto_stop_listen();
 		sequence_free();
 		gc_free();
 		engine_shutdown();
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index bb7d2b868..0bd3f7a4f 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -2015,6 +2015,22 @@ iproto_listen(const char *uri)
 	iproto_do_cfg(&cfg_msg);
 }
 
+
+/**
+ * Stop binary service.
+ *
+ * We don't have a proper iproto shutdown, iproto thread
+ * just dies and never stops listening on socket. This leaves
+ * dead unix sockets after tarantool. So we have to manually
+ * call evio_service_stop() to unink unix socket, if any.
+ * When iproto_shutdown() will be implemented, this function
+ * won't be needed anymore
+ */
+void
+iproto_stop_listen(void) {
+	evio_service_stop(&binary);
+}
+
 size_t
 iproto_mem_used(void)
 {
diff --git a/src/box/iproto.h b/src/box/iproto.h
index b9a6cf8f7..304af3b67 100644
--- a/src/box/iproto.h
+++ b/src/box/iproto.h
@@ -77,6 +77,9 @@ iproto_init();
 void
 iproto_listen(const char *uri);
 
+void
+iproto_stop_listen(void);
+
 void
 iproto_set_msg_max(int iproto_msg_max);
 
diff --git a/test/app-tap/tarantoolctl.test.lua b/test/app-tap/tarantoolctl.test.lua
index 6946c8312..1ba31ccf6 100755
--- a/test/app-tap/tarantoolctl.test.lua
+++ b/test/app-tap/tarantoolctl.test.lua
@@ -113,6 +113,14 @@ local function tctl_wait(dir, name)
     end
 end
 
+local function wait_delete(path)
+    if path then
+        while fio.path.exists(path) do
+            fiber.sleep(0.001)
+        end
+    end
+end
+
 local function tctl_command(dir, cmd, args, name)
     local pid = nil
     if not fio.stat(fio.pathjoin(dir, '.tarantoolctl')) then
@@ -150,7 +158,7 @@ local function check_ok(test, dir, cmd, args, e_res, e_stdout, e_stderr)
 end
 
 local test = tap.test('tarantoolctl')
-test:plan(6)
+test:plan(7)
 
 -- basic start/stop test
 -- must be stopped afterwards
@@ -250,6 +258,44 @@ do
     end
 end
 
+-- check enter
+do
+    local dir = fio.tempdir()
+
+    local code = [[ box.cfg{} ]]
+    create_script(dir, 'script.lua', code)
+
+    local status, err = pcall(function()
+        test:test("check error codes in case of enter", function(test_i)
+            test_i:plan(10)
+            check_ok(test_i, dir, 'enter', 'script', 1, nil, "Can't connect to")
+            local console_sock = 'script.control'
+            console_sock = fio.pathjoin(dir, console_sock)
+            test_i:is(fio.path.exists(console_sock), false, "directory clean")
+            check_ok(test_i, dir, 'start', 'script', 0)
+            tctl_wait(dir, 'script')
+            test_i:is(fio.path.exists(console_sock), true,
+                      "unix socket created")
+            check_ok(test_i, dir, 'stop', 'script', 0)
+            --wait_delete(console_sock)
+            test_i:is(fio.path.exists(console_sock), false,
+	              "remove unix socket upon exit")
+            fio.open(console_sock, 'O_CREAT')
+	    test_i:is(fio.path.exists(console_sock), true, "file created")
+            check_ok(test_i, dir, 'enter', 'script', 1, nil, "Can't connect to")
+            fio.unlink(console_sock)
+        end)
+    end)
+
+    cleanup_instance(dir, 'script')
+    recursive_rmdir(dir)
+
+    if status == false then
+        print(("Error: %s"):format(err))
+        os.exit()
+    end
+end
+
 -- check basic help
 do
     local dir = fio.tempdir()
-- 
2.15.2 (Apple Git-101.1)

[-- Attachment #2: Type: text/html, Size: 15442 bytes --]

      reply	other threads:[~2018-08-03  8:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-16  8:26 [tarantool-patches] " Serge Petrenko
2018-07-18  5:19 ` [tarantool-patches] " Konstantin Osipov
2018-07-18  9:19   ` Sergey Petrenko
2018-08-03  8:52     ` Sergey Petrenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E88A69A0-D00A-4C9D-AA94-7A1425C50897@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH] tarantoolctl: return an error on enter to a dead socket.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox