lucky
lucky copied to clipboard
Using a param inside of a spawn doesn't work
This code:
class SomeAction < BrowserAction
param code : String
get "/some-action" do
spawn { puts code }
head 201
end
end
results in this error at runtime:
Unhandled exception in spawn:
Closed stream (IO::Error)
from /usr/local/Cellar/crystal/0.26.1/src/io.cr:128:5 in 'check_open'
from /usr/local/Cellar/crystal/0.26.1/src/io/memory.cr:219:5 in 'gets_to_end'
from lib/lucky/src/lucky/params.cr:303:5 in 'body'
from lib/lucky/src/lucky/params.cr:240:23 in 'form_params'
from lib/lucky/src/lucky/params.cr:223:7 in 'body_param'
from lib/lucky/src/lucky/params.cr:47:32 in 'get?'
from src/actions/some_action.cr:2:3 in 'code'
from src/actions/some_action.cr:4:3 in '->'
from /usr/local/Cellar/crystal/0.26.1/src/fiber.cr:255:3 in 'run'
from /usr/local/Cellar/crystal/0.26.1/src/fiber.cr:29:34 in '->'
I assume this has something to do with the param method being lazy evaluated or something. So when it's in the other thread, calling that code method calls in to the IO that no longer exists.
This is easily solved by doing
get "/some-action" do
c = code
spawn { puts c }
head 201
end
However, maybe this is an easy fix? Or maybe a prettier error message like ParameterException?