From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 9BB321F934 for ; Mon, 24 May 2021 14:14:19 +0000 (UTC) Received: by mail-lf1-x133.google.com with SMTP id q7so39541628lfr.6 for ; Mon, 24 May 2021 07:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=3aLk/NDJUbJCFXdVoHWfBzV8PLVshJjHmHdYvnJuwcQ=; b=oH9fv1rYxfWy/EScw9B5impbwroEQgzyEaa71AReUJgjQTF9pVWK8LOaypXR9IyuPc /YjO2H/NQQ5h/uUaFbg+0p7TeyNn1eMS5XS3NbIho8Fx5Rf49gb/jQexKDAoelW93V0C cHmBHCgd+jUAw0ltfN5IC4bv+P7ks+iQqfx9wV4g5UZXP6XndQq60za6yTv5L7YddmPK 8MlubtS213B4tBgF+Hp+9ms2fAKwa6wbheAW2c/zHFq55D8m3cIc9a31v8QPdkFq9Zxh 0p0AaJigKbIlhGODfaRPSRN1AoRLud49IaYgUJekW6Yz++qfbDhgQLsuWKbroybdAzKt rxTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=3aLk/NDJUbJCFXdVoHWfBzV8PLVshJjHmHdYvnJuwcQ=; b=e0eJtDMwtfhKNSLgGPEUuFzl25691P1lFLv5TuAKIvcviYA3/3nZlevfbrKiPXnYwA xxSpiq+Lm10Lpb83krPznoA4h3QNiAZlKbMqc8uYIDECciADy3uxfkkkaAd6CQ3PPOse ovlehS9NLsUW0sJ2fB8ZYR1BhrYdb6xYMbFnw0raOoWMbl+IccgN7UowS1Bo57GAUiG9 hOJgZx6W+RmlEHGZt44Iwl7lwtKi5w95kr81mW0qXy4WRHKU/mKodRmgjformJLqpDrX Z+Qm0gzJ7yEKNPWCjzn95bWrkOHdABwr0mgwiuTLwIP2hHREZHlJ01zMfYG64FnjouEy 1HdA== X-Gm-Message-State: AOAM533wQ9+EWeQRrjP5c8xXpkSm0h5DE8LbVzD3Sc1NHBIRoIh+Uyj7 5OokeUlWuE9odi3wL9Y/7w24WJGm4NcFWuZMpCjLzTaSPqpqUg== X-Google-Smtp-Source: ABdhPJzt4nmpPL17//2TqKKGmfVbVC64Bnja/drk8qqTTZVsfhwByDshi5oRlNEL6TUkZwz0hlQpihf0AR0zRI/3V9k= X-Received: by 2002:a05:6512:1315:: with SMTP id x21mr10611831lfu.87.1621865652101; Mon, 24 May 2021 07:14:12 -0700 (PDT) MIME-Version: 1.0 From: Ngan Pham Date: Mon, 24 May 2021 07:14:01 -0700 Message-ID: Subject: Patch for GC.compact memory issue To: kgio-public@yhbt.net Content-Type: text/plain; charset="UTF-8" List-Id: Hello, I'd like to submit a patch to fix an issue after running `GC.compact`. The LOCALHOST variable is defined in Ruby and is resolved and memoized (to `localhost`) in C once. However, the pointer in C is not GC marked. Running `GC.compact` potentially moves the `LOCALHOST` object in Ruby causing `localhost` to point to a bad or empty slot. This causes `env['REMOTE_ADDR']` to a bad object in Unicorn. Some useful links: - `GC.compact` introduced: https://bugs.ruby-lang.org/issues/15626. - My conversation with tenderlove over twitter on this issue: https://twitter.com/tenderlove/status/1396625048861954049 Thanks, Ngan diff --git a/ext/kgio/accept.c b/ext/kgio/accept.c index f1de39d..5a5f15a 100644 --- a/ext/kgio/accept.c +++ b/ext/kgio/accept.c @@ -497,7 +497,14 @@ void init_kgio_accept(void) */ rb_define_const(mKgio, "SOCK_CLOEXEC", INT2NUM(SOCK_CLOEXEC)); + /* + * Resolve LOCALHOST from Ruby land and memoize it for fast lookup. + * We have to GC mark the object since it's defined in Ruby and we + * don't want GC.compact to move it. + */ localhost = rb_const_get(mKgio, rb_intern("LOCALHOST")); + rb_gc_register_mark_object(localhost); + cKgio_Socket = rb_const_get(mKgio, rb_intern("Socket")); cClientSocket = cKgio_Socket; mSocketMethods = rb_const_get(mKgio, rb_intern("SocketMethods"));