-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Reloading w/ Rails in Development #651
Comments
I had a similar problem, so I tried to fix it in #632 . I just pushed |
I upgraded to |
😖 I'm sorry to hear that. I wonder if it's the same bug or a different one. Could you help me debug a bit? It would be really helpful to me if you could capture the entire terminal output from:
(The entire output would be helpful because, in my case, the hang was caused by a previous error) Also, could you share your Rails version? |
Some environments relation to graphql:
A shortest hang log:
Then it hung. I run dtruss to watch what is it doing, the dtruss log at: https://gist.github.com/cpunion/10ef55375362cde05126b32dd6cd4d62, seems it dead loop to load some modules. It's not same with @Randuin 's issue. |
I try to reproduce @Randuin 's issue, and now it didn't hang, just report LoadError. |
Thanks, that's really helpful! I think the cause may be similar: an error during the |
Oh, hung again. I added |
Great! thanks. (restarted tens today 😄 ) |
Hmm... I tried a simple case to reproduce the error you posted above: Types::MutationType = GraphQL::ObjectType.define do
name "Mutation"
field :invalid do
type(nil)
end
end But it didn't cause the server to hang. I was able to modify the Ruby file & successfully re-send the query. Could you share the source of your schema and one of the files that has a validation error? |
I created a repo to reproduce https://github.com/cpunion/reproduce_graphql_hang Steps:
|
@rmosolgo I tried your code, also reproduced, just test with above steps. Refresh twice, fix code (change nil to types.Int) and refresh again, then it hung. |
The simplest steps: just refresh once then fix code and refresh again. |
Thanks, I can replicate it locally now, I'm looking into it! It seems like it never even makes it to the controller body, somehow it's getting stuck in Rails constant loading, hmmm |
@rmosolgo Can confirm ^^. Like you said it doesn't even reach to controller body, log shows post to |
Same here. Havent spent any time debugging it yet. |
What an adventure! It looks like # config/application.rb
if Rails.env.development?
# This isn't threadsafe -- it will work on single-threaded development servers only.
ALREADY_REMOVED = Set.new
module ActiveSupport::Dependencies
class << self
alias :old_remove_constant :remove_constant
def remove_constant(const_name)
if ALREADY_REMOVED.include?(const_name)
# pass
else
ALREADY_REMOVED.add(const_name)
old_remove_constant(const_name)
end
end
end
end
class UnloadCyclesMiddleware
def initialize(app, opts = {})
@app = app
end
def call(env)
ALREADY_REMOVED.clear
@app.call(env)
end
end
end
# ...
module MyApp
class Application < Rails::Application
# ...
config.middleware.insert_before(ActionDispatch::Reloader, UnloadCyclesMiddleware)
end
end Now, to find a real fix! |
Great! You save me. Thanks! How did you find the reason for this kind of problems? I tried, but no idea. I don't know how to trace what is it working when it hang, is there a tool/command to find the stack of threads? |
I'm not a very sophisticated debugger, here's what I did:
So in the end, I can't really fix it, but I can prevent it! |
Thank you. I have benefited from your thorough explanation. |
Hung again just now:( I upgraded to v1.5.6 and it works fine, until a syntax errors:
Seems the dtruss log is same as above. My commentable_type.rb defines an InterfaceType, uses in shared_type.rb with |
It's basically the same problem, but in this case, GraphQL's involvement is coincidental. It would be nice to see if it can be reduced, then we could open a bug with Rails (or, if we can't, maybe we can find something else to fix here 😆 ) |
I just ran into this here and was quite surprised. Maybe it's the way that GraphQL DSL is set up? Do we need to use # app/graphql/types/query_type.rb
module Types::QueryType
extend GraphQL::ObjectType
name "Query"
description "The query root of this schema"
# Add root-level fields here.
# They will be entry points for queries on your schema.
field :post do
type Types::PostType
argument :id, !types.ID
description "Find a Post by ID"
resolve ->(obj, args, ctx) { Post.find_by_id(args["id"]) }
end
end |
Hoping to make a change like that soon! Discussion at #820 |
I forked the reproduction repo above and migrated it to the new class-based API and it still hangs But I really want to fix this, so ... not giving up yet :S :S :S (edit: posted the wrong SHA) |
Is there a way to fix this for development only? Working with this problem is crazy and I'm loosing my mind. Please... Everytime I need to kill and restart the server!!!! |
@johnunclesam we know this is an issue for some users and we can image the frustration if you have to restart your server for every change. Have you tried the workaround as proposed in #651 (comment)? |
Caching up here, seeing that #1037 was merged, and there's a doc for class based schemas: https://github.com/rmosolgo/graphql-ruby/blob/1fa7a2a4/guides/schema/class_based_api.md I'll give this a shot too, looking great. 💯 |
@qrush note that class based schema isn't released yet, there might be some sharp edges! |
@qrush, the class based schemas fix my problem? |
@Willianvdv I tried now. Bad results:
All of this is frustrating. |
😅 I finally spent some more time investigating this, TL;DR:
# config/initializers/autoloaded_constants_patch.rb
if Rails.env.development?
# Work around a constant unloading issue, see:
# https://github.com/rails/rails/issues/31694
# https://github.com/rmosolgo/graphql-ruby/issues/651
# Use this modified Array class instead of Set
# because Set doesn't implement some methods
# required by ActiveSupport::Dependencies
class UniqueArray < Array
def <<(other)
if include?(other)
self
else
super(other)
end
end
end
ActiveSupport::Dependencies.autoloaded_constants = UniqueArray.new
end Can someone give a try with that workaround and let me know what you find? I followed the same steps which I described above, and found basically the same result. But I spent more time with |
@rmosolgo you are GOD. This workaround now works! AMAZIIIIIIIINGGGG!!!!!! Thanks! We'll wait for a Rails team feedback now. Thanks again. |
Thank you very much for this patch. I had an infinite loop problem caused by a syntax error in one of my types (UserType) that was being incorrectly reported as a problem with |
@rmosolgo seeing rails/rails#31803, can we remove this workaround? From wich Rails version? 5.2? |
@johnunclesam I upgraded to 5.1.5 and seems it be fixed. I think it fixed in 5.2.0-rc1. |
Fixed on latest rails 🎉 |
Hi @rmosolgo , In which version this is supposed to be fixed?. we are using rails 5.1.6 and server it's still hanging when code in backend is updated. |
Hi @rmosolgo , I'm currently using: And the backend still hangs when I update the code. |
Using some of the very basic examples I'm running into an issue where query execution would hang whenever I change code in development. And even if I don't change code, on all subsequent requests I would get the error
LoadError (Unable to autoload constant Types::QueryType, expected /Users/randuin/code/pw/pwpback/app/graphql/types/query_type.rb to define it):
The text was updated successfully, but these errors were encountered: