Repository navigation
Description
I have some Rails 7.2 applications working fine with JRuby however some exeptions are not reported to log file, stdout, or browser screen in development
I managed to get the apps working fine with the following monkey patch
config/initializers/monkeypatch_rails_exception_wrapper.rb
unless RUBY_PLATFORM == 'java'
# This monkey patch is not needed for not java platform
return
end
module ActionDispatch
class ExceptionWrapper
private
def build_backtrace
built_methods = {}
ActionView::PathRegistry.all_resolvers.each do |resolver|
resolver.built_templates.each do |template|
built_methods[template.method_name] = template
end
end
# Below line is required for java platform, native java exceptions don't
# define :backtrace_locations
return [] unless @exception&.respond_to?(:backtrace_locations)
(@exception.backtrace_locations || []).map do |loc|
if built_methods.key?(loc.label.to_s)
thread_backtrace_location = if loc.respond_to?(:__getobj__)
loc.__getobj__
else
loc
end
SourceMapLocation.new(thread_backtrace_location, built_methods[loc.label.to_s])
else
loc
end
end
end
end
end
# This constant is defined only in CRuby
module RubyVM
class AbstractSyntaxTree
end
endThe monkey patched code is located in:
exception wrapper
Environment Information:
- Ubuntu 24.10
- Java 21
Reproduction steps:
I created and minimal Rails 7.2 app with CRuby and postgres database, modified the Gemfile to
run the app with CRuby and JRuby
rails new wallaby --minimal --skip-test -d postgresql
I scaffolded some sample pages with JRuby only, here is the app
When application has not issue everything works fine however when exception comes from Java then Rails is not able to report the exception for example:
- when database does not exist or invalid password, rails does not report anything
JRuby
(bash)> bundle exec rails s
=> Booting Puma
=> Rails 7.2.2.1 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.6.0 ("Return to Forever")
* Ruby version: jruby 9.4.9.0 (3.1.4) 2024-11-04 547c6b150e OpenJDK 64-Bit Server VM 21.0.6+7-Ubuntu-124.10.1 on 21.0.6+7-Ubuntu-124.10.1 +jit [x86_64-linux]
* Min threads: 3
* Max threads: 3
* Environment: development
* PID: 3305569
* Listening on http://[::1]:3000
* Listening on http://127.0.0.1:3000
Use Ctrl-C to stop
Started GET "/" for 0:0:0:0:0:0:0:1 at 2025-03-01 11:49:35 +1100
CRuby
(bash)> bundle exec rails s
=> Booting Puma
=> Rails 7.2.2.1 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.6.0 ("Return to Forever")
* Ruby version: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-linux]
* Min threads: 3
* Max threads: 3
* Environment: development
* PID: 3304890
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop
Started GET "/" for ::1 at 2025-03-01 11:48:04 +1100
ActiveRecord::NoDatabaseError (We could not find your database: wallaby_development. Available database configurations can be found in config/database.yml.
To resolve this error:
- Did you not create the database, or did you delete it? To create the database, run:
bin/rails db:create
- Has the database name changed? Verify that config/database.yml contains the correct database name.
):
Causes:
PG::ConnectionBad (connection to server at "127.0.0.1", port 5432 failed: FATAL: database "wallaby_development" does not exist
)
- When there some invalid SQL queries such as not existing columns, table, etc.
- Rails with JRuby does not report anything (nothing in the log file or stdout and just internal error in the browser)
- Rails with CRuby reports the SQL issue in the log file and/or web browser
Additional information
In the sample app web app sample we have the following
masterbranch, app can be run in JRuby or Cruby where we can see the issues described above in JRubywatch_rails_failbranch, I added a controller to see where Rails failswatch_rails_with_monkeypatchbranch, the app same as master branch with the monkey patch working perfectly fine with JRuby, the errors are reported successfully to the log file or stdout or web browser
The sample app use the activerecord-jdbc-alt-adapter gem, the code to support postres is almost the same as activerecord-jdbc-adapter
gem 'activerecord-jdbc-alt-adapter', '~> 72.0.0.rc1'
This issue is not specific to postgres, it can be seen in sqlite, mysql, etc.
Activity
[-]Rails 7.1 or 7.2 fails to report some errors on JRuby[/-][+]Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4.9.0[/+]enebo commentedon Feb 28, 2025
MRI records a node_id for each node when making the tree using the legacy and Prism parsers. These ids get associated with instructions in iseqs. The feature looks up current locations iseq then finds appropriate node_id. Then it parses the source again to find chunk of code to error highlight.
In JRuby both parsers need to add this. Additionally I don't think prism serialized format saves node_id so it needs to be added on prism side (unless it has been added already). Adding the new node_id for our legacy parser is just adding logic in base constructor.
Storage likely will be a side car and not adding a field to every instruction in our IR.
The only thing i don't know is how it re-renders the different ASTs into printable source again. This could be another project in itself since we have no version of RubyVM::AbstractSyntaxTree but I need to look at how that is done.
[-]Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4.9.0[/-][+]Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4.x.y[/+]JesseChavez commentedon Mar 1, 2025
Here is the sample app with the monkey patch successfully reporting the issue.
[-]Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4.x.y[/-][+]Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4[/+]chadlwilson commentedon May 23, 2026
Interestingly, via jruby-rack on 10.0/10.1, in a similar case I do get something reported rather than nothing; it's just that the original error is lost.
Perhaps the complete loss of anything is puma-related; or due to some other middleware in between; or its different when the main error is a Ruby one, with a cause that is the Java Throwable as in your case.
Same root cause though - looks like this would be resolved by #9458 being implemented.
An alternate monkey-patch with opinionated logging is at https://github.com/chadlwilson/warbler/blob/e14e9982a56818f4ae919152de8acbef7ed8abba/integration/rails7_test/src/main/ruby/config/initializers/java_throwable_backtrace_locations.rb - although probably a bad approach, was just throwaway for some tests.
kares commentedon Oct 10, 2026
this has been resolved, previously with #9458