Skip to content

Rails 7.1 or 7.2 fails to report some errors on JRuby 9.4 #8665

Closed

Description

@JesseChavez

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
end

The 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

web app sample

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:

  1. 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
)
  1. 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

  • master branch, app can be run in JRuby or Cruby where we can see the issues described above in JRuby
  • watch_rails_fail branch, I added a controller to see where Rails fails
  • watch_rails_with_monkeypatch branch, 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

  1. changed the title [-]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[/+] on Feb 28, 2025
  2. enebo commented on Feb 28, 2025

    @enebo
    Member

    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.

  3. changed the title [-]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[/+] on Feb 28, 2025
  4. JesseChavez commented on Mar 1, 2025

    @JesseChavez
    Author

    Here is the sample app with the monkey patch successfully reporting the issue.

    (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: 3309381
    * 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:56:35 +1100
      
    ActiveRecord::NoDatabaseError (org.postgresql.util.PSQLException: FATAL: database "wallaby_development" does not exist):
    
    Causes:
    Java::OrgPostgresqlUtil::PSQLException (FATAL: database "wallaby_development" does not exist)
      
    arjdbc/jdbc/RubyJdbcConnection.java:534:in `initialize'
    org/jruby/RubyClass.java:935:in `new'
    activerecord-jdbc-alt-adapter-72.0.0.rc1 (java) lib/arjdbc/postgresql/adapter.rb:891:in `new_client'
    activerecord-jdbc-alt-adapter-72.0.0.rc1 (java) lib/arjdbc/abstract/connection_management.rb:43:in `connect'
    activerecord-jdbc-alt-adapter-72.0.0.rc1 (java) lib/arjdbc/abstract/connection_management.rb:53:in `reconnect'
    activerecord (7.2.2.1) lib/active_record/connection_adapters/abstract_adapter.rb:662:in `block in reconnect!'
    
  5. changed the title [-]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[/+] on Mar 1, 2025
  6. chadlwilson commented on May 23, 2026

    @chadlwilson
    Contributor

    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.

    NoMethodError (undefined method 'backtrace_locations' for an instance of Java::JavaLang::NoSuchMethodError):
    
    actionpack (7.2.3.1) lib/action_dispatch/middleware/exception_wrapper.rb:269:in 'build_backtrace'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/exception_wrapper.rb:58:in 'initialize'
    org/jruby/RubyClass.java:1044:in 'new'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/executor.rb:27:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/debug_exceptions.rb:31:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/show_exceptions.rb:32:in 'call'
    railties (7.2.3.1) lib/rails/rack/logger.rb:41:in 'call_app'
    railties (7.2.3.1) lib/rails/rack/logger.rb:29:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/remote_ip.rb:96:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/request_id.rb:33:in 'call'
    rack (2.2.23) lib/rack/method_override.rb:24:in 'call'
    rack (2.2.23) lib/rack/runtime.rb:22:in 'call'
    activesupport (7.2.3.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/server_timing.rb:61:in 'block in call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/server_timing.rb:26:in 'collect_events'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/server_timing.rb:60:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/executor.rb:16:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/static.rb:27:in 'call'
    rack (2.2.23) lib/rack/sendfile.rb:127:in 'call'
    actionpack (7.2.3.1) lib/action_dispatch/middleware/host_authorization.rb:143:in 'call'
    railties (7.2.3.1) lib/rails/engine.rb:535:in 'call'
    org/jruby/rack/ext/Servlet.java:78:in 'call'
    

    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.

  7. kares commented on Oct 10, 2026

    @kares
    Member

    this has been resolved, previously with #9458

  8. added this to the Invalid or Duplicate milestone on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions