kgio RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* Patch for GC.compact memory issue
@ 2021-05-24 14:14 Ngan Pham
  2021-05-24 17:22 ` Eric Wong
  0 siblings, 1 reply; 8+ messages in thread
From: Ngan Pham @ 2021-05-24 14:14 UTC (permalink / raw)
  To: kgio-public

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"));

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-05-25 22:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24 14:14 Patch for GC.compact memory issue Ngan Pham
2021-05-24 17:22 ` Eric Wong
2021-05-24 19:00   ` Ngan Pham
2021-05-24 20:33     ` Eric Wong
2021-05-24 21:11       ` Ngan Pham
2021-05-24 21:48         ` [PATCH] HACKING: update docs with "git request-pull" info Eric Wong
2021-05-25 16:41         ` Patch for GC.compact memory issue Aaron Patterson
2021-05-25 22:50           ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/kgio.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).