You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then, I called stop method on a task created with task.async to stop the http server, like:
defstop@server_task&.stopend
and, I got a NoMethodError error.
Is there something wrong with my code?
Please see Reproduction code and Result sections in below.
Environment
Ruby 3.3.4
async 2.16.1
async-http 0.71.0
Reproduction code
require'bundler/inline'gemfiledosource'https://rubygems.org'gem'async-http'endrequire'net/http'require'async/http/protocol'require'timeout'moduleHttpServerclassRouterdefinitialize@router={get: {}}enddefmount(method,path,app)@router[method][path]=appenddefroute!(method,path,request)@router.fetch(method).fetch(path).call(request)endendclassServerclassAppdefinitialize(router)@router=routerenddefcall(request)method=request.methodresp=get(request)Protocol::HTTP::Response[*resp]rescue=>eProtocol::HTTP::Response[500,{'Content-Type'=>'text/plain'},'Internal Server Error']enddefget(request)@router.route!(:get,request.path,request)endenddefinitialize(addr:,port:,tls_context: nil)@uri=URI("http://#{addr}:#{port}").to_s@router=Router.new@server_task=nil@server=Async::HTTP::Server.new(App.new(@router),Async::HTTP::Endpoint.parse(@uri))enddefstartAsyncdo |task|
@server_task=task.asyncdo@server.runendendenddefstop@server_task&.stopenddefget(path,app=nil, &block)@router.mount(:get,path,app || block)endendenddefhttp_server_start(addr:,port:)server=HttpServer::Server.new(addr: addr,port: port)server.get('/api/plugins.json'){ |req| [200,{'Content-Type'=>'text/html'},"Hello"]}Thread.newdoserver.startendsleep1# Wait until the server starts up.serverend#################################################puts"[http server start]"server=http_server_start(addr: "127.0.0.1",port: "8080")server.stop
Result
$ ruby async-http.rb
[http server start]
/home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/task.rb:288:in `stop': private method `raise' called for nil (NoMethodError)
Fiber.scheduler.raise(@fiber, Stop)
^^^^^^
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/node.rb:291:in `block in stop_children'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/list.rb:290:in `each'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/list.rb:303:in `each'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/list.rb:178:in `each'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/node.rb:290:in `stop_children'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/task.rb:401:in `stopped!'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/task.rb:414:in `stop!'
from /home/watson/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/async-2.16.1/lib/async/task.rb:296:in `stop'
from t.rb:63:in `stop'
from t.rb:91:in `<main>'
The text was updated successfully, but these errors were encountered:
If you want to invoke stop across threads, you wil need to use an appropriate mechanism, e.g.
def start
@stop = Thread::Queue.new
Async do |task|
@server_task = task.async do
@server.run
end
@stop.pop
@server_task.stop
end
end
def stop
@stop.push(true)
end
I didn't test this, but something like this will probably work.
I'm trying to launch an Async HTTP server like following:
Then, I called
stop
method on a task created withtask.async
to stop the http server, like:and, I got a
NoMethodError
error.Is there something wrong with my code?
Please see
Reproduction code
andResult
sections in below.Environment
Reproduction code
Result
The text was updated successfully, but these errors were encountered: