Skip to content

Commit a378705

Browse files
ericproulxclaude
andcommitted
Define Grape's callable constants as modules rather than lambdas
A proc carries the self it was written under and the variables it closes over, so freezing one does not make it shareable; a module is shareable as it is. DEFAULT_LAMBDA_FORMATTER, FORWARD_MATCH_METHOD, NON_FORWARD_MATCH_METHOD and RUN_ENDPOINT become modules answering call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8a908c1 commit a378705

5 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎lib/grape/endpoint/rack_stack.rb‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ class Endpoint
88
# compile time, from the endpoint's settings.
99
module RackStack
1010
# The innermost app of every endpoint's Rack stack. The endpoint to run
11-
# comes out of the env, so the lambda holds no state and one of them serves
12-
# every stack, rather than one being allocated per endpoint.
13-
RUN_ENDPOINT = ->(env) { env[Grape::Env::API_ENDPOINT].run }
14-
private_constant :RUN_ENDPOINT
11+
# comes out of the env, so the app holds no state and one of them serves
12+
# every stack, rather than one being allocated per endpoint. A module
13+
# rather than a lambda, which carries the self it was written under and
14+
# so cannot be frozen all the way down.
15+
module RunEndpoint
16+
def self.call(env) = env[Grape::Env::API_ENDPOINT].run
17+
end
18+
private_constant :RunEndpoint
1519

1620
module_function
1721

@@ -27,7 +31,7 @@ def build(setting)
2731
stack.use Grape::Middleware::Formatter, **formatter_middleware_options(setting, format, content_types)
2832

2933
builder = stack.build
30-
builder.run RUN_ENDPOINT
34+
builder.run RunEndpoint
3135
builder.to_app
3236
end
3337

‎lib/grape/formatter.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ module Formatter
66

77
module_function
88

9-
DEFAULT_LAMBDA_FORMATTER = ->(obj, _env) { obj }
9+
# Answers the body as it is, for a format nothing is registered for. A
10+
# module like the formatters registered here, rather than a lambda: a
11+
# lambda carries the self it was written under, so it cannot be frozen
12+
# all the way down.
13+
DEFAULT_LAMBDA_FORMATTER = Module.new do
14+
def self.call(obj, _env) = obj
15+
end
1016

1117
def formatter_for(api_format, formatters)
1218
return formatters[api_format] if formatters&.key?(api_format)

‎lib/grape/router/route.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
module Grape
44
class Router
55
class Route < BaseRoute
6-
FORWARD_MATCH_METHOD = ->(input, pattern) { input.start_with?(pattern.origin) }
7-
NON_FORWARD_MATCH_METHOD = ->(input, pattern) { pattern.match?(input) }
6+
# Modules rather than lambdas, which carry the self they were written
7+
# under and so cannot be frozen all the way down.
8+
FORWARD_MATCH_METHOD = Module.new do
9+
def self.call(input, pattern) = input.start_with?(pattern.origin)
10+
end
11+
12+
NON_FORWARD_MATCH_METHOD = Module.new do
13+
def self.call(input, pattern) = pattern.match?(input)
14+
end
815

916
attr_reader :app, :request_method, :index
1017

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
describe Grape do
4+
# A proc carries the self it was written under and the variables it closes
5+
# over, so freezing it does not let a non-main Ractor call it. A callable
6+
# held in a constant is a module, or an object frozen all the way down.
7+
describe 'its constants holding a callable' do
8+
it 'can be called from a non-main Ractor' do
9+
expect(GrapeConstants.failing { |value| !value.respond_to?(:call) || Ractor.shareable?(value) }).to be_empty
10+
end
11+
end
12+
end

‎spec/support/grape_constants.rb‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# Walks the constants Grape's lib defines, private ones included, to check
4+
# what a non-main Ractor could read. Constants still to autoload are the files
5+
# Grape leaves out of eager loading, which would load Rails, and are skipped.
6+
module GrapeConstants
7+
LIB = File.expand_path('../../lib', __dir__)
8+
9+
module_function
10+
11+
# The names of the constants, modules aside, whose value the block answers
12+
# falsy for.
13+
def failing
14+
Grape.deprecator.silence do
15+
grape_modules.flat_map { |mod| constants_of(mod) }.filter_map do |name, value|
16+
name unless value.is_a?(Module) || yield(value)
17+
end
18+
end
19+
end
20+
21+
def grape_modules
22+
ObjectSpace.each_object(Module).select do |mod|
23+
name = name_of(mod)
24+
name == 'Grape' || name&.start_with?('Grape::')
25+
end
26+
end
27+
28+
def constants_of(mod)
29+
constant_names_of(mod).filter_map do |name|
30+
next unless defined_in_lib?(mod, name)
31+
32+
["#{name_of(mod)}::#{name}", mod.const_get(name, false)]
33+
end
34+
end
35+
36+
# Module#constants leaves private constants out, so their names are read
37+
# from the source.
38+
def constant_names_of(mod)
39+
mod.constants(false) | private_constant_names.select { |name| mod.const_defined?(name, false) }
40+
end
41+
42+
def private_constant_names
43+
Dir["#{LIB}/**/*.rb"]
44+
.flat_map { |file| File.read(file).scan(/private_constant\s+([^\n#]+)/).flatten }
45+
.flat_map { |names| names.scan(/:(\w+)/).flatten }
46+
.uniq.map(&:to_sym)
47+
end
48+
49+
def defined_in_lib?(mod, name)
50+
!mod.autoload?(name) && mod.const_source_location(name)&.first&.start_with?(LIB)
51+
end
52+
53+
def name_of(mod)
54+
Module.instance_method(:name).bind_call(mod)
55+
end
56+
end

0 commit comments

Comments
 (0)