質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

セグメンテーション違反

セグメンテーション違反とは、ソフトウェア実行時に発生するエラーのひとつであり、許可されていないメモリにアクセスしたときに起きます。しばしば、ポインタの不適切な使用、またはバッファオーバーフローによって起こります。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

1310閲覧

EC2でセグメンテーションエラー【unicornが原因】

Baru_blog

総合スコア0

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

セグメンテーション違反

セグメンテーション違反とは、ソフトウェア実行時に発生するエラーのひとつであり、許可されていないメモリにアクセスしたときに起きます。しばしば、ポインタの不適切な使用、またはバッファオーバーフローによって起こります。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

0クリップ

投稿2021/02/18 11:32

前提・実現したいこと

現在、EC2にアプリをデプロイしている最中です。

$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D コマンドでエラーが発生しました。(unicron起動のコマンド)

発生している問題・エラーメッセージ

Refreshing Gem list /home/name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/unicorn-5.8.0/lib/unicorn.rb:80: [BUG] Segmentation fault at 0x0000000000000000 ruby 3.0.0p0

該当のソースコード

unicorn

1 2# config/unicorn.rb 3 4app_path = File.expand_path('..', __dir__) 5 6worker_processes 1 7 8working_directory app_path 9 10pid "#{app_path}/tmp/pids/unicorn.pid" 11 12listen 3000 13 14stderr_path "#{app_path}/log/unicorn.stderr.log" 15 16stdout_path "#{app_path}/log/unicorn.stdout.log" 17 18timeout 60 19 20preload_app true 21 22check_client_connection false 23 24run_once = true 25 26before_fork do |server, worker| 27 defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! 28 29 if run_once 30 run_once = false 31 end 32 33 old_pid = "#{server.config[:pid]}.oldbin" 34 if File.exist?(old_pid) && server.pid != old_pid 35 begin 36 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 37 Process.kill(sig, File.read(old_pid).to_i) 38 rescue Errno::ENOENT, Errno::ESRCH => e 39 logger.error e 40 end 41 end 42end 43 44after_fork do |_server, _worker| 45 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection 46end 47

下記がエラーメッセージに表記されているコードです。

unicorn

1# /home/name/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/unicorn-5.8.0/lib/unicorn.rb 2 3# -*- encoding: binary -*- 4require 'etc' 5require 'stringio' 6require 'kgio' 7require 'raindrops' 8require 'io/wait' 9 10begin 11 require 'rack' 12rescue LoadError 13 warn 'rack not available, functionality reduced' 14end 15 16# :stopdoc: 17# Unicorn module containing all of the classes (include C extensions) for 18# running a Unicorn web server. It contains a minimalist HTTP server with just 19# enough functionality to service web application requests fast as possible. 20# :startdoc: 21 22# unicorn exposes very little of an user-visible API and most of its 23# internals are subject to change. unicorn is designed to host Rack 24# applications, so applications should be written against the Rack SPEC 25# and not unicorn internals. 26module Unicorn 27 28 # Raised inside TeeInput when a client closes the socket inside the 29 # application dispatch. This is always raised with an empty backtrace 30 # since there is nothing in the application stack that is responsible 31 # for client shutdowns/disconnects. This exception is visible to Rack 32 # applications unless PrereadInput middleware is loaded. This 33 # is a subclass of the standard EOFError class and applications should 34 # not rescue it explicitly, but rescue EOFError instead. 35 ClientShutdown = Class.new(EOFError) 36 37 # :stopdoc: 38 39 # This returns a lambda to pass in as the app, this does not "build" the 40 # app (which we defer based on the outcome of "preload_app" in the 41 # Unicorn config). The returned lambda will be called when it is 42 # time to build the app. 43 def self.builder(ru, op) 44 # allow Configurator to parse cli switches embedded in the ru file 45 op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op) 46 if ru =~ /.ru$/ && !defined?(Rack::Builder) 47 abort "rack and Rack::Builder must be available for processing #{ru}" 48 end 49 50 # always called after config file parsing, may be called after forking 51 lambda do |_, server| 52 inner_app = case ru 53 when /.ru$/ 54 raw = File.read(ru) 55 raw.sub!(/^__END__\n.*/, '') 56 eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru) 57 else 58 require ru 59 Object.const_get(File.basename(ru, '.rb').capitalize) 60 end 61 62 if $DEBUG 63 require 'pp' 64 pp({ :inner_app => inner_app }) 65 end 66 67 return inner_app unless server.default_middleware 68 69 middleware = { # order matters 70 ContentLength: nil, 71 Chunked: nil, 72 CommonLogger: [ $stderr ], 73 ShowExceptions: nil, 74 Lint: nil, 75 TempfileReaper: nil, 76 } 77 78 # return value, matches rackup defaults based on env 79 # Unicorn does not support persistent connections, but Rainbows! 80 # and Zbatery both do. Users accustomed to the Rack::Server default 81 # middlewares will need ContentLength/Chunked middlewares. 82 case ENV["RACK_ENV"] 83 when "development" 84 when "deployment" 85 middleware.delete(:ShowExceptions) 86 middleware.delete(:Lint) 87 else 88 return inner_app 89 end 90 Rack::Builder.new do 91 middleware.each do |m, args| 92 use(Rack.const_get(m), *args) if Rack.const_defined?(m) 93 end 94 run inner_app 95 end.to_app 96 end 97 end 98 99 # returns an array of strings representing TCP listen socket addresses 100 # and Unix domain socket paths. This is useful for use with 101 # Raindrops::Middleware under Linux: https://yhbt.net/raindrops/ 102 def self.listener_names 103 Unicorn::HttpServer::LISTENERS.map do |io| 104 Unicorn::SocketHelper.sock_name(io) 105 end + Unicorn::HttpServer::NEW_LISTENERS 106 end 107 108 def self.log_error(logger, prefix, exc) 109 message = exc.message 110 message = message.dump if /[[:cntrl:]]/ =~ message 111 logger.error "#{prefix}: #{message} (#{exc.class})" 112 exc.backtrace.each { |line| logger.error(line) } 113 end 114 F_SETPIPE_SZ = 1031 if RUBY_PLATFORM =~ /linux/ 115 116 def self.pipe # :nodoc: 117 Kgio::Pipe.new.each do |io| 118 io.close_on_exec = true # remove this when we only support Ruby >= 2.0 119 120 # shrink pipes to minimize impact on /proc/sys/fs/pipe-user-pages-soft 121 # limits. 122 if defined?(F_SETPIPE_SZ) 123 begin 124 io.fcntl(F_SETPIPE_SZ, Raindrops::PAGE_SIZE) 125 rescue Errno::EINVAL 126 # old kernel 127 rescue Errno::EPERM 128 # resizes fail if Linux is close to the pipe limit for the user 129 # or if the user does not have permissions to resize 130 end 131 end 132 end 133 end 134 # :startdoc: 135end 136# :enddoc: 137 138%w(const socket_helper stream_input tee_input http_request configurator 139 tmpio util http_response worker http_server).each do |s| 140 require_relative "unicorn/#{s}" 141end 142

Gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '3.0.0' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 6.1.0' 8# MySQL 9gem 'mysql2' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', :require => false 30# 多言語化する 31gem 'rails-i18n' 32# ページネーションを実装する 33gem 'kaminari' 34# 画像アップロード 35gem 'carrierwave' 36# 画像のリサイズ 37gem 'rmagick' 38 39group :development, :test do 40 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 41 gem 'byebug', :platforms => [:mri, :mingw, :x64_mingw] 42 # テストツール、RSpecの導入 43 gem 'rspec-rails', '~> 4.0.2' 44 # RSpecの高速化 45 gem 'spring-commands-rspec' 46 # RSpecをDRYにする 47 gem 'factory_bot_rails' 48 # CircleCI 49 gem 'rspec_junit_formatter' 50end 51 52group :development do 53 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 54 gem 'web-console', '>= 4.1.0' 55 # Display performance information such as SQL time and flame graphs for each request in your browser. 56 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 57 gem 'listen', '~> 3.3' 58 gem 'rack-mini-profiler', '~> 2.0' 59 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 60 gem 'spring' 61 # rubocopの導入 62 gem 'rubocop', :require => false 63 gem 'rubocop-performance', :require => false 64 gem 'rubocop-rails', :require => false 65 gem 'rubocop-rspec' 66end 67 68group :test do 69 # Adds support for Capybara system testing and selenium driver 70 gem 'capybara', '>= 3.26' 71 gem 'selenium-webdriver' 72 # Easy installation and use of web drivers to run system tests with browsers 73 gem 'webdrivers' 74end 75 76group :production do 77 gem 'unicorn', '5.8.0' 78end 79 80# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 81gem 'tzinfo-data', :platforms => [:mingw, :mswin, :x64_mingw, :jruby] 82

試したこと

・ EC2のインスタンスメモリをt2.microからアップグレードした。(変化がなかったため、現在はt2.micro)
・ unicornのバージョンを下げる(Rubyのバージョンの関係で結局下げられなかった)

補足情報(FW/ツールのバージョンなど)

・ macOS
・ Rails 6.1.0
・ Ruby 3.0.0
・ unicron 5.8.0
・ nginx 1.18.0

参考記事

Qiita 1を参考にしてデプロイを進め、unicorn.rbの設定部分はQiita 2を参照しました。

Qiita 1
Qiita 2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。

原因はrubocopの自動修正によるもので、

app_path = File.expand_path('../../', dir)の相対パスが

app_path = File.expand_path('..', dir)になっていました。

凡ミスです。お恥ずかしい。

投稿2021/02/18 13:54

Baru_blog

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問