Thin doesn't respond to SIGINT or SIGTERM
My guess is that either something's tying up the EventMachine reactor loop preventing it from exiting, or something's trapping SIGINT.
As a simple example of the former, put this into config.ru
and run with thin -p 4567 start
:
require 'thin'
require 'sinatra'
require 'eventmachine'
get '/' do
"hello world"
end
run Sinatra::Application
EventMachine.schedule do
trap("INT") do
puts "Caught SIGINT"
EventMachine.stop # this is useless
# exit # this stops the EventMachine
end
i = 0
while i < 10
puts "EM Running"
i += 1
sleep 1
end
end
Without trapping the SIGINT, you get the same behavior as when trapping it and calling EM.stop. EM.stop (at least in the pure ruby version, which you can run with EVENTMACHINE_LIBRARY="pure_ruby" thin start
) sets a flag that a stop has been requested, which is picked up inside the reactor loop. If the reactor loop is stuck on a step (as in the above case), then it won't exit.
So a couple options:
use the workaround above of trapping SIGINT and forcing an exit. This could leave connections in an unclean state, but they don't call it quick & dirty for nothing ;)
you could put the blocking code inside a Thread or a Fiber, which will allow the reactor to keep running.
look for long-running tasks or loops inside your code, and convert these to be EventMachine aware. em-http-request is a great library for external http requests, and em-synchrony has several other protocols (for database connections, tcp connection pools, etc.). In the above example, this is straightforward:
EventMachine.add_periodic_timer(1) { puts "EM Running" }
In your actual code, this might be harder to track down, but look for any places where you spawn threads and join them, or large loops. A profiling tool can help show what code is running when you try to exit, and lastly you can try disabling various parts of the system and libraries to figure out where the culprit is.
Catching SIGTERM vs catching SIGINT
From https://en.wikipedia.org/wiki/Unix_signal:
SIGINT
is generated by the user pressing Ctrl+C and is an interrupt
SIGTERM
is a signal that is sent to request the process terminates. The kill command sends a SIGTERM
and it's a terminate
You can catch both SIGTERM
and SIGINT
and you will always be able to close the process with a SIGKILL
or kill -9 [pid]
.
Sinatra/Thin runs and cannot be stopped with Ctrl-C
This issue is specific to newer versions of Thin (notice that v1.5.1 does not exhibit this behavior). This behavior was introduced in 1.6 and a similar issue is documented here.
The code in question follows the same pattern as mentioned in the upstream issue.
TL;DR version of the issue: Thin will stop the server, but will not stop the reactor loop (because it does not "own" the reactor). It is possible to allow Thin to own its reactor loop, in which case you will get back the desired behavior (as seen in 1.5.1). In order to do this you must start Thin without the enclosing EM#run { }
, which will allow Thin to bring up (and subsequently tear down) the reactor loop.
In this case, one can imagine the periodic "ping" as a separate application that shares the reactor loop with Thin. Neither of them can claim ownership of the reactor loop. It would be wrong for Thin to stop all other applications and exit the reactor when it did not start the reactor. It is then the users responsibility to handle signals and terminate individual applications as necessary and finally stop the reactor loop (causing the process to quit).
Hope this explanation helps!
Message/logging from Thin
Those messages does not come from rack, they come from thin: https://github.com/macournoyer/thin/blob/master/lib/thin/server.rb#L150 You can set the logging preferences according to this: https://github.com/macournoyer/thin/blob/master/lib/thin/logging.rb Thin::Logging.silent = true, but do you really want to silence all? Maybe direct it to a log file instead of stdout?
Thin Foreman stopped working
Add the gem 'thin'
in your application Gemfile
and execute the bundle exec thin start
to resolve the version conflict problem.
Production environment THIN is not loading data
So the problem was the config.force_ssl parameter in config/environments/production.rb, it was in "true", but in the new server I didnt have the SSL configuration ready yet, so I changed it to false and it worked!
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false
I hope it helps!
Cheers
Python how to recieve SIGINT in Docker to stop service?
Solution:
The app must run as PID 1 inside docker to receive a SIGINT. To do so, one must use ENTRYPOINT instead of CMD. The fixed Dockerfile:
FROM python:3.8-slim-buster
COPY test/TestGS.py .
ENTRYPOINT ["python", "TestGS.py"]
Build the image:
docker build . -t python-signals
Run the image:
docker run -it --rm --name="python-signals" python-signals
And from a second terminal, stop the container:
docker stop python-signals
Then you get the expected output:
Received SIGTERM signal
Stop app
It seems a bit odd to me that Docker only emits SIGTERMS to PID 1, but thankfully that's relatively easy to fix. The article below was most helpful to solve this issue.
https://itnext.io/containers-terminating-with-grace-d19e0ce34290
Rails Thin server unresponsive
You are starting thin in threaded mode. Did you uncomment config.threadsafe! in your rails config so that it plays well with a threaded server?
Related Topics
Have a Parent Class's Method Access the Subclass's Constants
How to Use Fiddle to Pass or Return a Struct to Native Code
Sqlite3-Ruby Gem Can't Find SQLite3.H on Ubuntu
Solving Dependency Constraints
Superclass Mismatch, Struct, Reloading and Spork
How to Declare a Rake Task That Depends on a Parameterized Task
Undefined Method 'Devise' When Including User Model Outside Rails
Ruby Imap "Changes" Since Last Check
Using CSS to Style Ruby Objects in Erb
What Are These Numbers in Goto Anything of Sublime Text 2
How to Restart Rails from Within Rails
How to Store a Ruby Array into a File
How to Remove All Characters in a String Until a Substring Is Matched, in Ruby