Skip to content

before_x middleware seems to be triggering twice #757

@drum445

Description

@drum445

When using before_all (which I am currently using for auth checks), I noticed my DB calls were duplicating. I checked into this and the cause seems to be these middleware filters are firing twice.

Example:

require "kemal"

before_all do |env|
  puts "RUNNING THIS FOR #{env.request.path}; #{env.object_id}"
end

get "/" do |env|
  "hello, home"
end

get "/about" do |env|
  "hello, about"
end

Kemal.run

Output:

RUNNING THIS FOR /; 131141490140544
RUNNING THIS FOR /; 131141490140544
2026-06-06T11:50:46.948687Z   INFO - kemal: 200 GET / 3.32ms
RUNNING THIS FOR /about; 131141490139392
RUNNING THIS FOR /about; 131141490139392
2026-06-06T11:50:48.969343Z   INFO - kemal: 200 GET /about 3.44ms

I've tried swapping the order of route declarations and before_all, but the same result happens.

What does seem to work is using this instead, but I'm unsure about why this would cause the difference:

before_all "/*" do |env|
  puts "RUNNING THIS FOR #{env.request.path}; #{env.object_id}"
end

But the issue now is this doesn't trigger for "/"

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions