[#119000] [Ruby master Bug#20710] Reducing Hash allocation introduces large performance degradation (probably related to VWA) — "pocke (Masataka Kuwabara) via ruby-core" <ruby-core@...>

Issue #20710 has been reported by pocke (Masataka Kuwabara).

6 messages 2024/09/02

[#119033] [Ruby master Bug#20713] Ruby 3.3.5 triggers a deprecation warning with `require "json"` — "Bo98 (Bo Anderson) via ruby-core" <ruby-core@...>

Issue #20713 has been reported by Bo98 (Bo Anderson).

7 messages 2024/09/04

[#119041] [Ruby master Bug#20714] Handle optional dependencies in `bundled_gems.rb` — "Earlopain (A S) via ruby-core" <ruby-core@...>

Issue #20714 has been reported by Earlopain (A S).

31 messages 2024/09/04

[#119074] [Ruby master Bug#20716] Different instance_method behavior in Ruby 2.7 and Ruby 3.x — "natton (Tien Truong) via ruby-core" <ruby-core@...>

Issue #20716 has been reported by natton (Tien Truong).

13 messages 2024/09/06

[#119145] [Ruby master Misc#20728] Propose Eileen Uchitelle as a core committer — "kddnewton (Kevin Newton) via ruby-core" <ruby-core@...>

Issue #20728 has been reported by kddnewton (Kevin Newton).

14 messages 2024/09/12

[#119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal — "ursm (Keita Urashima) via ruby-core" <ruby-core@...>

Issue #20738 has been reported by ursm (Keita Urashima).

16 messages 2024/09/13

[#119199] [Ruby master Bug#20742] Trying to assign to a variable in statement modifier should emit a warning — "esad (Esad Hajdarevic) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNzQyIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGVzYWQgKEVzYWQgSGFqZGFyZXZpYyku

7 messages 2024/09/15

[#119208] [Ruby master Bug#20745] IO::Buffer#copy triggers UB when src/dest buffers overlap — "hanazuki (Kasumi Hanazuki) via ruby-core" <ruby-core@...>

Issue #20745 has been reported by hanazuki (Kasumi Hanazuki).

8 messages 2024/09/16

[#119239] [Ruby master Feature#20750] Expose ruby_thread_has_gvl_p in ruby/thread.h — "kbrock (Keenan Brock) via ruby-core" <ruby-core@...>

Issue #20750 has been reported by kbrock (Keenan Brock).

8 messages 2024/09/17

[#119248] [Ruby master Bug#20752] IO::Buffer#slice fails to copy readonly flag, allowing writes into frozen String — "hanazuki (Kasumi Hanazuki) via ruby-core" <ruby-core@...>

Issue #20752 has been reported by hanazuki (Kasumi Hanazuki).

7 messages 2024/09/18

[#119301] [Ruby master Bug#20761] [DOC] `RubyVM::AbstractSyntaxTree.of` examples raise because parser is prism by default — "Earlopain (A S) via ruby-core" <ruby-core@...>

Issue #20761 has been reported by Earlopain (A S).

11 messages 2024/09/26

[#119335] [Ruby master Bug#20770] A *new* pipe operator proposal — "AlexandreMagro (Alexandre Magro) via ruby-core" <ruby-core@...>

Issue #20770 has been reported by AlexandreMagro (Alexandre Magro).

56 messages 2024/09/29

[ruby-core:119132] Segfault using ruby C on MacOS (Intel Catalina and M2 Sonoma)

From: "martin.kufner--- via ruby-core" <ruby-core@...>
Date: 2024-09-12 08:13:15 UTC
List: ruby-core #119132
Hey guys, 
I was experienced at C about 20 years ago, since 15 years I'm coding with Ruby.

I now need to write a Ruby C extension, and I fail with just calling simple commands like rb_p.
I try to integrate a fast C websocket server into my quiz website (Rails cable is too slow)

In order to communicate I need a UNIX socket in C to receive commands from the Rails server.

I used the xcode standard compiler, and also tried homebrew gcc-14
It compiles fine all set up with extconf.rb

When I call QbGameServer.init, it creates the server, and after a second wait tries to connect.
The c thread is created and gets the message,
However when I call rb_p or rb_funcall it hangs (M2) or segfauls (Intel)

What do I do wrong?

Thanks a lot!
Martin


I created a QbGameServerModule, extending my QbGameServer and call
module QbGameServer
SOCKET_PATH="/tmp/sock/qb-game-server"
  extend QbGameServerModule
def self.init
    # Start the server (non-blocking)
    self.start_rpc(SOCKET_PATH) do |message|
      puts "Received from client: #{message}"
    end
    sleep 1
    send_socket
  end


  def self.send_socket
    # Create a new Unix socket
    socket = UNIXSocket.new(SOCKET_PATH)

    # Example data to send to the server (Ruby hash converted to JSON)
    data_to_send = { message: "Hello, server!", timestamp: Time.now.to_s }.to_json

    # Send the JSON data to the server
    puts "Sent to server: #{data_to_send}"
    socket.puts(data_to_send)

    # Read the response from the server
    response = socket.gets.chomp # Read the response from the socket
    puts "Received from server: #{response}"

  ensure
    # Close the socket connection after the communication is done
    socket.close if socket

  end
end

On the C side  I have the ext/qb_game_server/qb_game_server.c

#include "ruby.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024

VALUE QbGameServerModule = Qnil;

static VALUE rpc_proc = Qnil;
static char socket_file_path[108]; // Maximum length for UNIX domain socket path

void *client_handler(void *sock) {
    int client_socket = *(int *)sock;
    char buffer[BUFFER_SIZE];
    ssize_t bytes_read;

    while ((bytes_read = read(client_socket, buffer, sizeof(buffer) - 1)) > 0) {
        buffer[bytes_read] = '\0';
        printf("Received C %s\n", buffer);
        VALUE arg = rb_str_new_cstr(buffer);
//        rb_gc_register_address(&arg);
        printf("1\n"); // DOES REACH
//        rb_gc();
        printf("2\n"); // DOES REACH
        rb_p(arg);
// ***** DOES NOT REACH HERE*****
// On the osx sonoma M2 it just hangs, on osx Intel Catalina it shows a segfault
        printf("3\n"); 



//        rb_gc_unregister_address(&arg);
//        rb_funcall(rpc_proc, rb_intern("call"), 1, arg);

    }

    close(client_socket);
    return NULL;
}

void *server_thread(void *arg) {
    int server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
    struct sockaddr_un server_addr;
    int client_sockets[MAX_CLIENTS];
    pthread_t threads[MAX_CLIENTS];
    int client_count = 0;

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sun_family = AF_UNIX;
    strncpy(server_addr.sun_path, socket_file_path, sizeof(server_addr.sun_path) - 1);

    unlink(socket_file_path);
    bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
    listen(server_socket, MAX_CLIENTS);

    while (1) {
        client_sockets[client_count] = accept(server_socket, NULL, NULL);
        pthread_create(&threads[client_count], NULL, client_handler, &client_sockets[client_count]);
        client_count++;
    }

    close(server_socket);
    return NULL;
}

static VALUE start_rpc(VALUE self, VALUE socket_file) {
    Check_Type(socket_file, T_STRING);
    strncpy(socket_file_path, StringValueCStr(socket_file), sizeof(socket_file_path) - 1);

    if (rb_block_given_p()) {
        rpc_proc = rb_block_proc();
    } else {
        rb_raise(rb_eArgError, "A block must be provided as a callback");
    }

    pthread_t server_tid;
    pthread_create(&server_tid, NULL, server_thread, NULL);
    return Qnil;
}

void Init_qb_game_server() {
    printf("Init QbGameServerModule\n");
    QbGameServerModule = rb_define_module("QbGameServerModule");
    rb_define_method(QbGameServerModule, "start_rpc", start_rpc, 1);
}
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/


In This Thread

Prev Next