Fix Rack::Timeout::RequestTimeoutException: Request ran for longer than 15000ms in Rails
This error means a request exceeded the configured Rack::Timeout threshold. Long-running requests block web server threads and degrade performance for all users. Move expensive operations to background jobs using Sidekiq or ActiveJob, optimize slow database queries, and add pagination to endpoints that return large datasets.
Reading the Stack Trace
Here's what each line means:
- rack-timeout (0.7.0) lib/rack/timeout/core.rb:120:in `call': Rack::Timeout raised this exception because the request exceeded the 15-second threshold.
- app/controllers/reports_controller.rb:15:in `generate': The generate action is performing expensive work inline instead of in a background job.
- app/models/report.rb:45:in `build_annual_report': The build_annual_report method runs complex queries that take too long for a web request.
Common Causes
1. Expensive computation in request cycle
A report generation query takes too long to run synchronously during a web request.
def generate
@report = Report.build_annual_report(params[:year])
render json: @report
end
2. Unoptimized database query
A query without proper indexes or with N+1 issues causes the request to time out.
def index
@orders = Order.all.map do |order|
{ order: order, items: order.line_items.map(&:name) }
end
end
3. External API call without timeout
A synchronous call to an external service with no timeout blocks the request.
def verify
response = Net::HTTP.get(URI('https://slow-api.example.com/verify'))
render json: response
end
The Fix
Move the expensive report generation to a background job. Return a 202 Accepted response immediately and notify the user when the report is ready. This keeps web requests fast and prevents timeouts.
def generate
@report = Report.build_annual_report(params[:year])
render json: @report
end
def generate
job = ReportJob.perform_later(params[:year], current_user.id)
render json: { status: 'processing', job_id: job.provider_job_id }, status: :accepted
end
# app/jobs/report_job.rb
class ReportJob < ApplicationJob
queue_as :reports
def perform(year, user_id)
report = Report.build_annual_report(year)
user = User.find(user_id)
ReportMailer.completed(user, report).deliver_later
end
end
Testing the Fix
require 'rails_helper'
RSpec.describe ReportsController, type: :request do
describe 'POST /reports/generate' do
let(:user) { create(:user) }
before { sign_in user }
it 'returns accepted status' do
post '/reports/generate', params: { year: 2024 }
expect(response).to have_http_status(:accepted)
end
it 'enqueues a report job' do
expect {
post '/reports/generate', params: { year: 2024 }
}.to have_enqueued_job(ReportJob)
end
end
end
Run your tests:
bundle exec rspec spec/requests/reports_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-rack-timeout,git add app/controllers/reports_controller.rb app/jobs/report_job.rb,git commit -m "fix: move report generation to background job to prevent timeout",git push origin fix/rails-rack-timeout
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
redis:
image: redis:7
ports: ['6379:6379']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- run: bin/rails db:setup
- run: bundle exec rspec
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
gem install bugstack
Step 2: Initialize
require 'bugstack'
Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Move the expensive operation to a background job.
- Add request specs verifying the async response.
- Configure Sidekiq queues for the new job.
- Open a pull request.
- Merge and monitor job processing in staging.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Most web requests should complete in under 5 seconds. Set Rack::Timeout to 15 seconds as a safety net and move anything taking longer than 3 seconds to a background job.
Enable Rack::Timeout logging and integrate with your APM tool. Track the wait and service time metrics to identify slow endpoints.