Techioz Blog

Docker化されたアプリケーションでのRuby on Railsエラー「スタックスマッシングが検出されました」を解決する方法

概要

私の M2 MacBook Pro 16 インチ、2023 上

Mac オペレーティング システムを 13.4.1 (c) (22F770820d) に更新するまで動作していた Dockerized Rails アプリがあります。

ここで、再構築してデプロイしたアプリコンテナの開発ページをロードすると、次の出力でシャットダウンします。

2023-07-24 16:03:33    (0.1ms)  SELECT pg_try_advisory_lock(3213395767516697805);
2023-07-24 16:03:33   ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
2023-07-24 16:03:33   ActiveRecord::InternalMetadata Load (0.2ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
2023-07-24 16:03:33    (0.1ms)  BEGIN
2023-07-24 16:03:33    (0.1ms)  COMMIT
2023-07-24 16:03:33    (0.1ms)  SELECT pg_advisory_unlock(3213395767516697805)
2023-07-24 16:03:33   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
2023-07-24 16:03:36 *** stack smashing detected ***: /usr/local/bundle/bin/rake assets:precompile terminated
2023-07-24 16:03:36 ./entrypoint.sh: line 5:    17 Aborted                 bundle exec rake assets:precompile
2023-07-24 16:03:36 Puma starting in single mode...
2023-07-24 16:03:36 * Version 3.12.1 (ruby 2.5.1-p57), codename: Llamas in Pajamas
2023-07-24 16:03:36 * Min threads: 5, max threads: 5
2023-07-24 16:03:36 * Environment: development
2023-07-24 16:03:38 * Listening on tcp://0.0.0.0:3000
2023-07-24 16:03:38 Use Ctrl-C to stop
2023-07-24 16:03:44 Started GET "/" for 10.0.0.2 at 2023-07-24 16:03:44 -0400
2023-07-24 16:03:44 Cannot render console from 10.0.0.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
2023-07-24 16:03:44   ActiveRecord::SchemaMigration Load (0.6ms)  SELECT "schema_migrations".* FROM "schema_migrations"
2023-07-24 16:03:44 Processing by PagesController#splash as HTML
2023-07-24 16:03:44   Rendering pages/splash.haml within layouts/application
2023-07-24 16:03:44 *** stack smashing detected ***: puma 3.12.1 (tcp://0.0.0.0:3000) [app] terminated
2023-07-24 16:03:44 ./entrypoint.sh: line 11:    29 Aborted                 bundle exec puma -C config/puma.rb

Docker リソースを天文学的な数字にまで増やしてみましたが、うまくいきませんでした。私の主要な Docker ファイルの一部を以下に示します。

docker-compose.local.yml:

version: '3.7'
services:
  web:
    image: game-fuse_web
    build:
      context: .
      dockerfile: ./Dockerfile.web.local
    restart: always
    depends_on:
      - app
    volumes:
      - '.:/var/www/game-fuse-ast'
    ports:
      - 80:80

  db:
    image: postgres:9.6-bullseye
    env_file:
      - .env
    volumes:
      - "db:/var/lib/postgresql/data"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=game-fuse_development
    ports:
      - "5432:5432"
    

  app:
    stdin_open: true
    tty: true
    image: game-fuse_app
    build:
      context: .
      dockerfile: ./Dockerfile.local
    command: ./entrypoint.sh
    volumes:
      - .:/app
    ports:
      - 3000:3000
    working_dir: /app
    depends_on:
      - db

    
volumes:
  db:

Dockerfile.local:

FROM ruby:2.5.1-slim

ENV TZ=America/New_York


# Allow recently archived stretch to be accessed

RUN echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list && \
    sed -i 's/deb.debian.org/debian.osuosl.org/g' /etc/apt/sources.list && \
    sed -i 's/security.debian.org/debian.osuosl.org/g' /etc/apt/sources.list


RUN apt-get update -qq && apt-get install -y build-essential cron libpq-dev nodejs git
RUN gem install nokogiri -v 1.12.5
RUN gem install net-protocol -v 0.1.2
RUN gem install net-smtp -v 0.3.0
RUN gem install net-imap -v 0.2.2
#RUN gem install -N rails

# Set an environment variable where the Rails app is installed to inside of Docker image
ENV RAILS_ROOT /var/www/game-fuse

RUN mkdir -p $RAILS_ROOT

# Set working directory
WORKDIR $RAILS_ROOT

# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'

# Remove reliance on git:// protocol
RUN git config --global url."https://".insteadOf git://

# Adding gems
COPY Gemfile Gemfile 
COPY Gemfile.lock Gemfile.lock
COPY config ./config

# Adding project files
RUN gem install bundler -v 2.3.26

RUN bundle install --jobs 20 --retry 5 

RUN cd $RAILS_ROOT && bundle exec whenever --update-crontab

エントリポイント.sh:

#!/bin/bash

bundle exec rake db:migrate

bundle exec rake assets:precompile

#bundle exec whenever --update-crontab

#cron

bundle exec puma -C config/puma.rb

Ruby on Rails または Docker のコンテキストでインターネット上でこのエラーについてどこを調べればよいのかよくわかりません。また、検索結果もあまりありません。

解決策

「スタックの破壊が検出されました」は、puma がバッファ オーバーフローに陥っていることを示しており、プログラムがデータの漏洩や破損からユーザーを保護しています。これはおそらく、コンパイルされた Puma バイナリ拡張からのものであり、Ruby や Rails からのものではありません。

3.12.1 は Puma の非常に古いバージョンです。まずはアップデートしてみることをお勧めします。