Skip to content

MetaCall Ruby Runtime Crash with Python Integration #563

Open
@MoX678

Description

@MoX678

MetaCall Ruby Runtime Crash with Python Integration

Expected Behavior

The main.js script should successfully load the Python module weather_analytics.py, execute the get_weather_stats function, and return the analytics data without crashing the Ruby runtime.

Current Behavior

when i run metacall main.js it loads files it crashes The logs show:
A Python error: Module 'encoding' not found.
A Ruby error: [BUG] Segmentation fault.
changing package versions or deleting Ruby runtimes did not resolve the issue !

NOTES

If the following lines are moved to main.js, the crash occurs:

require("metacall");
const weather = require('../weather_analytics.py');
const analytics = await weather.get_weather_stats(weatherData);

or (another form that also causes the crash):

  const { metacall_load_from_file } = require("metacall");
  metacall_load_from_file("py", ["weather_analytics.py"]);

However, placing this same code in the handlers/weather-analyze module works correctly and loads the Python script without issues.

Error Logs

Python Initialization Module 'encoding' not found
Error: Handle 0000000000000000 passed to metacall_handle_export is not valid
Ruby Crash: Segmentation fault

* Loaded script: ruby

* Loaded features:

    0 enumerator.so
    1 thread.rb
    2 fiber.so
    3 rational.so
    4 complex.so
    5 ruby2_keywords.rb

Stack trace (most recent call last):
The specified module could not be found.

#31   Object "", at 00007FF91B0843F0, in  ??
#30   Object "", at 00007FF99AEE3970, in AES_cbc_encrypt
#29   Object "", at 00007FF99AF722CE, in AES_cbc_encrypt
#28   Object "", at 00007FF99A2E8070, in AES_cbc_encrypt
#27   Object "", at 00007FF99A2E820F, in AES_cbc_encrypt
#26   Object "", at 00007FF99A2E7E2A, in AES_cbc_encrypt
#25   Object "", at 00007FF99A2E839E, in AES_cbc_encrypt
#24   Object "", at 00007FF9993F764F, in AES_cbc_encrypt
#23   Object "", at 00007FFAD5439AC0, in node_loader_impl_load_from_memory
#22   Object "", at 00007FFAD56B78A8, in metacall_load_from_file
#21   Object "", at 00007FFAD56B3803, in loader_load_from_file
#20   Object "", at 00007FFAD56B54BF, in loader_impl_load_from_file
#19   Object "", at 00007FFAB26E316B, in rb_loader_impl_load_from_file
#18   Object "", at 00007FFAB26E3306, in rb_loader_impl_load_from_file
#17   Object "", at 00007FFA36066981, in rb_define_module
#16   Object "", at 00007FFA360841AC, in rb_raise
#15   Object "", at 00007FFA36084CAA, in rb_vraise
#14   Object "", at 00007FFA3607D32E, in rb_exc_raise
#13   Object "", at 00007FFA3607D304, in rb_error_write
#12   Object "", at 00007FFA3607D7AC, in rb_keyword_given_p
#11   Object "", at 00007FFA36052388, in rb_reserved_fd_p
#10   Object "", at 00007FFAEE7E5BDE, in KiUserExceptionDispatcher
#9    Object "", at 00007FFAEE6F45D7, in RtlWow64GetCurrentCpuArea
#8    Object "", at 00007FFAEE7E629F, in _chkstk
#7    Object "", at 00007FFAEBB51705, in _C_specific_handler_noexcept
#6    Object "", at 00007FFAEBB5156F, in _C_specific_handler
#5    Object "", at 00007FFAEBB8E235, in memcpy
#4    Object "", at 00007FFAEBB2E8A4, in seh_filter_exe
#3    Object "", at 00007FFA3619045E, in ruby_signal_name
#2    Object "", at 00007FFA3608330F, in rb_bug_for_fatal_signal
#1    Object "", at 00007FFA36081C8B, in ruby_stop
#0    Object "", at 00007FFAEBB44AB1, in abort

Code Snippets

Python: weather_analytics.py

import statistics
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
def get_weather_stats(weather_data):
    """Enhanced function using polynomial regression for better accuracy."""
    temps = [item["main"]["temp"] for item in weather_data["list"]]
    timestamps = np.arange(len(temps)).reshape(-1, 1)

    if len(temps) < 3:
        return {"error": "Not enough data to predict", "language": "Python"}

    # Use Polynomial Regression (degree 2 for better curve fitting)
    model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())
    model.fit(timestamps, temps)

    # Predict next day's temperature
    next_time_index = np.array([[len(temps)]])
    predicted_temp = round(model.predict(next_time_index)[0], 2)
    return {
        "average_temperature": round(np.mean(temps), 2),
        "max_temp": round(np.max(temps), 2),
        "min_temp": round(np.min(temps), 2),
        "predicted_temp_next_day": round(predicted_temp, 2),
        "trend": trend,
        "summary": weather_summary["summary"],   
        "condition": condition
    }

requirements.txt

numpy==1.23.5
scikit_learn==1.6.1

main.js

require("metacall");
const weather = require('../weather_analytics.py');
const analytics = await weather.get_weather_stats(weatherData);

Show Case

Recording.2025-06-04.014602.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions