Skip to content

Commit 3a9693b

Browse files
committed
[GR-48237] Backports for 23.1 batch 2
PullRequest: truffleruby/3984
2 parents b6fe7c8 + 7ae3f77 commit 3a9693b

File tree

16 files changed

+114
-104
lines changed

16 files changed

+114
-104
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Bug fixes:
1313
* Fix `super` method lookup for unbounded attached methods (#3131, @itarato).
1414
* Fix `Module#define_method(name, Method)` to respect `module_function` visibility (#3181, @andrykonchin).
1515
* Fix stack overflow with `Kernel.require` and `zeitwerk` (#3224, @eregon).
16+
* Reimplement `IO.select` with `poll(2)` to support file descriptors >= 1024 (#3201, @eregon).
1617

1718
Compatibility:
1819

common.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Jsonnet files should not include this file directly but use ci/common.jsonnet instead."
55
],
66

7-
"mx_version": "6.43.0",
7+
"mx_version": "6.45.0",
88

99
"COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet",
1010
"jdks": {

mx.truffleruby/mx_truffleruby.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,11 @@ def verify_ci(args):
297297
launchers=['bin/<exe:ruby>', 'bin/<exe:truffleruby>'],
298298
jar_distributions=['truffleruby:TRUFFLERUBY-LAUNCHER'],
299299
main_class='org.truffleruby.launcher.RubyLauncher',
300-
# Set Xmx to use a reliable amount of memory and avoid swapping
301300
build_args=[
302-
'-J-Xmx6g',
303301
'-H:+DetectUserDirectoriesInImageHeap',
304302
],
305303
# G1 is only supported on linux currently
306-
build_args_enterprise=(['-J-Xmx8g', '--gc=G1', '-H:-ProtectionKeys'] if (mx.get_os() == 'linux' and 'NATIVE_IMAGE_AUXILIARY_ENGINE_CACHE' not in os.environ) else ['-J-Xmx7g']),
304+
build_args_enterprise=(['--gc=G1', '-H:-ProtectionKeys'] if (mx.get_os() == 'linux' and 'NATIVE_IMAGE_AUXILIARY_ENGINE_CACHE' not in os.environ) else []),
307305
language='ruby',
308306
option_vars=[
309307
'RUBYOPT',

mx.truffleruby/suite.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
suite = {
2-
"mxversion": "6.42.0",
2+
"mxversion": "6.44.0",
33
"name": "truffleruby",
44
"groupId": "org.graalvm.ruby",
55
"url": "https://www.graalvm.org/ruby/",
@@ -20,7 +20,7 @@
2020
{
2121
"name": "regex",
2222
"subdir": True,
23-
"version": "4bcb7c9a9061d0948609412048a6906fa0cb826c",
23+
"version": "621ba026a108b8320e5cdadf6150c2fa30b1339a",
2424
"urls": [
2525
{"url": "https://github.com/oracle/graal.git", "kind": "git"},
2626
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"},
@@ -29,7 +29,7 @@
2929
{
3030
"name": "sulong",
3131
"subdir": True,
32-
"version": "4bcb7c9a9061d0948609412048a6906fa0cb826c",
32+
"version": "621ba026a108b8320e5cdadf6150c2fa30b1339a",
3333
"urls": [
3434
{"url": "https://github.com/oracle/graal.git", "kind": "git"},
3535
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"},
@@ -610,6 +610,7 @@
610610
"BSD-simplified", # MRI
611611
"BSD-new", # Rubinius, FFI
612612
],
613+
"compress": True,
613614
"maven": {
614615
"artifactId": "ruby-resources",
615616
"tag": ["default", "public"],

spec/ruby/core/io/select_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,26 @@
5555
end
5656
end
5757

58-
it "returns supplied objects correctly even when monitoring the same object in different arrays" do
59-
filename = tmp("IO_select_pipe_file") + $$.to_s
58+
it "returns supplied objects correctly when monitoring the same object in different arrays" do
59+
filename = tmp("IO_select_pipe_file")
6060
io = File.open(filename, 'w+')
6161
result = IO.select [io], [io], nil, 0
6262
result.should == [[io], [io], []]
6363
io.close
6464
rm_r filename
6565
end
6666

67+
it "returns the pipe read end in read set if the pipe write end is closed concurrently" do
68+
main = Thread.current
69+
t = Thread.new {
70+
Thread.pass until main.stop?
71+
@wr.close
72+
}
73+
IO.select([@rd]).should == [[@rd], [], []]
74+
ensure
75+
t.join
76+
end
77+
6778
it "invokes to_io on supplied objects that are not IO and returns the supplied objects" do
6879
# make some data available
6980
@wr.write("foobar")

spec/truffle/io/read_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
w.write "b"
3030
w.flush
3131

32-
select_ignoring_iobuffer([r], 1_000_000).should == [r]
32+
select_ignoring_iobuffer([r], 100).should == [r]
3333
r.read(1).should == "a"
3434

35-
select_ignoring_iobuffer([r], 1_000_000).should == [r]
35+
select_ignoring_iobuffer([r], 1000).should == [r]
3636
r.read(1).should == "b"
3737
end
3838

@@ -42,17 +42,17 @@
4242
@w.flush
4343

4444
@r.gets.should == "a\n"
45-
select_ignoring_iobuffer([@r], 100_000).should == nil
45+
select_ignoring_iobuffer([@r], 100).should == nil
4646
end
4747

48-
def select_ignoring_iobuffer(ios, timeout_us)
48+
def select_ignoring_iobuffer(ios, timeout_ms)
4949
return IO.select(ios)[0] unless defined?(::TruffleRuby)
5050

5151
result = Truffle::IOOperations.select(
5252
ios, ios,
5353
[], [],
5454
[], [],
55-
timeout_us, timeout_us)
55+
timeout_ms)
5656
result and result[0]
5757
end
5858
end

src/main/c/truffleposix/truffleposix.c

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ SUCH DAMAGE.
6060
#include <sys/file.h>
6161
#include <sys/resource.h>
6262
#include <sys/stat.h>
63-
#include <sys/select.h>
6463
#include <sys/time.h>
6564
#include <sys/types.h>
6665
#include <sys/wait.h>
@@ -91,27 +90,7 @@ struct truffleposix_stat {
9190

9291
static void copy_stat(struct stat *stat, struct truffleposix_stat* buffer);
9392

94-
static void init_fd_set(fd_set *set, int nfds, int *fds, int *maxfd) {
95-
FD_ZERO(set);
96-
for (int i = 0; i < nfds; i++) {
97-
int fd = fds[i];
98-
FD_SET(fd, set);
99-
if (fd > *maxfd) {
100-
*maxfd = fd;
101-
}
102-
}
103-
}
104-
105-
static void mark_ready_from_set(fd_set *set, int nfds, int *fds) {
106-
for (int i = 0; i < nfds; i++) {
107-
int fd = fds[i];
108-
if (!FD_ISSET(fd, set)) {
109-
fds[i] = -1;
110-
}
111-
}
112-
}
113-
114-
int truffleposix_poll(int fd, int events, int timeout_ms) {
93+
int truffleposix_poll_single_fd(int fd, int events, int timeout_ms) {
11594
struct pollfd fds;
11695

11796
fds.fd = fd;
@@ -120,32 +99,6 @@ int truffleposix_poll(int fd, int events, int timeout_ms) {
12099
return poll(&fds, 1, timeout_ms) >= 0 ? fds.revents : -1;
121100
}
122101

123-
int truffleposix_select(int nread, int *readfds, int nwrite, int *writefds,
124-
int nexcept, int *exceptfds, long timeout_us) {
125-
struct timeval timeout;
126-
struct timeval *timeout_ptr = NULL;
127-
128-
if (timeout_us >= 0) {
129-
timeout.tv_sec = (timeout_us / 1000000);
130-
timeout.tv_usec = (timeout_us % 1000000);
131-
timeout_ptr = &timeout;
132-
}
133-
134-
int maxfd = 0;
135-
fd_set readset, writeset, exceptset;
136-
init_fd_set(&readset, nread, readfds, &maxfd);
137-
init_fd_set(&writeset, nwrite, writefds, &maxfd);
138-
init_fd_set(&exceptset, nexcept, exceptfds, &maxfd);
139-
140-
int ret = select(maxfd+1, &readset, &writeset, &exceptset, timeout_ptr);
141-
if (ret > 0) {
142-
mark_ready_from_set(&readset, nread, readfds);
143-
mark_ready_from_set(&writeset, nwrite, writefds);
144-
mark_ready_from_set(&exceptset, nexcept, exceptfds);
145-
}
146-
return ret;
147-
}
148-
149102
int truffleposix_utimes(const char *filename, long atime_sec, int atime_nsec,
150103
long mtime_sec, int mtime_nsec) {
151104
struct timespec timespecs[2];

src/main/java/org/truffleruby/platform/DarwinAArch64NativeConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ public static void load(NativeConfiguration configuration, RubyContext context)
407407
configuration.config("platform.poll.POLLIN", 1);
408408
configuration.config("platform.poll.POLLPRI", 2);
409409
configuration.config("platform.poll.POLLOUT", 4);
410+
configuration.config("platform.poll.POLLERR", 8);
411+
configuration.config("platform.poll.POLLHUP", 16);
412+
configuration.config("platform.poll.POLLRDNORM", 64);
413+
configuration.config("platform.poll.POLLRDBAND", 128);
414+
configuration.config("platform.poll.POLLWRNORM", 4);
415+
configuration.config("platform.poll.POLLWRBAND", 256);
410416
configuration.config("platform.socket.AF_APPLETALK", 16);
411417
configuration.config("platform.socket.PF_APPLETALK", 16);
412418
configuration.config("platform.socket.AF_INET", 2);

src/main/java/org/truffleruby/platform/DarwinAMD64NativeConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ public static void load(NativeConfiguration configuration, RubyContext context)
407407
configuration.config("platform.poll.POLLIN", 1);
408408
configuration.config("platform.poll.POLLPRI", 2);
409409
configuration.config("platform.poll.POLLOUT", 4);
410+
configuration.config("platform.poll.POLLERR", 8);
411+
configuration.config("platform.poll.POLLHUP", 16);
412+
configuration.config("platform.poll.POLLRDNORM", 64);
413+
configuration.config("platform.poll.POLLRDBAND", 128);
414+
configuration.config("platform.poll.POLLWRNORM", 4);
415+
configuration.config("platform.poll.POLLWRBAND", 256);
410416
configuration.config("platform.socket.AF_APPLETALK", 16);
411417
configuration.config("platform.socket.PF_APPLETALK", 16);
412418
configuration.config("platform.socket.AF_INET", 2);

src/main/java/org/truffleruby/platform/LinuxAArch64NativeConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ public static void load(NativeConfiguration configuration, RubyContext context)
418418
configuration.config("platform.poll.POLLIN", 1);
419419
configuration.config("platform.poll.POLLPRI", 2);
420420
configuration.config("platform.poll.POLLOUT", 4);
421+
configuration.config("platform.poll.POLLERR", 8);
422+
configuration.config("platform.poll.POLLHUP", 16);
423+
configuration.config("platform.poll.POLLRDNORM", 64);
424+
configuration.config("platform.poll.POLLRDBAND", 128);
425+
configuration.config("platform.poll.POLLWRNORM", 256);
426+
configuration.config("platform.poll.POLLWRBAND", 512);
421427
configuration.config("platform.socket.AF_APPLETALK", 5);
422428
configuration.config("platform.socket.PF_APPLETALK", 5);
423429
configuration.config("platform.socket.AF_AX25", 3);

src/main/java/org/truffleruby/platform/LinuxAMD64NativeConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ public static void load(NativeConfiguration configuration, RubyContext context)
418418
configuration.config("platform.poll.POLLIN", 1);
419419
configuration.config("platform.poll.POLLPRI", 2);
420420
configuration.config("platform.poll.POLLOUT", 4);
421+
configuration.config("platform.poll.POLLERR", 8);
422+
configuration.config("platform.poll.POLLHUP", 16);
423+
configuration.config("platform.poll.POLLRDNORM", 64);
424+
configuration.config("platform.poll.POLLRDBAND", 128);
425+
configuration.config("platform.poll.POLLWRNORM", 256);
426+
configuration.config("platform.poll.POLLWRBAND", 512);
421427
configuration.config("platform.socket.AF_APPLETALK", 5);
422428
configuration.config("platform.socket.PF_APPLETALK", 5);
423429
configuration.config("platform.socket.AF_AX25", 3);

src/main/ruby/truffleruby/core/io.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,8 @@ def self.select(readables = nil, writables = nil, errorables = nil, timeout = ni
735735

736736
raise ArgumentError, 'timeout must be positive' if timeout < 0
737737

738-
# Microseconds, rounded down
739-
timeout = remaining_timeout = Integer(timeout * 1_000_000)
740-
else
741-
remaining_timeout = -1
738+
# Milliseconds, rounded down
739+
timeout = Integer(timeout * 1_000)
742740
end
743741

744742
if readables
@@ -782,7 +780,7 @@ def self.select(readables = nil, writables = nil, errorables = nil, timeout = ni
782780
readables, readable_ios,
783781
writables, writable_ios,
784782
errorables, errorable_ios,
785-
timeout, remaining_timeout)
783+
timeout)
786784
end
787785

788786
##

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
214214
attach_function :open, [:string, :int, varargs(:mode_t)], :int
215215
attach_function :opendir, [:string], :pointer
216216
attach_function :pipe, [:pointer], :int
217-
attach_function :truffleposix_poll, [:pointer, :long, :int], :int, LIBTRUFFLEPOSIX, true
217+
# blocking=false for both poll because the timeout needs to be decreased on EINTR
218+
attach_function :truffleposix_poll_single_fd, [:pointer, :long, :int], :int, LIBTRUFFLEPOSIX
219+
attach_function :poll, [:pointer, :long, :int], :int
218220
attach_function :read, [:int, :pointer, :size_t], :ssize_t, LIBC, true
219221
attach_function :readlink, [:string, :pointer, :size_t], :ssize_t
220222
attach_function :realpath, [:string, :pointer], :pointer
@@ -224,8 +226,6 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
224226
attach_function :truffleposix_rewinddir, [:pointer], :void, LIBTRUFFLEPOSIX
225227
attach_function :rmdir, [:string], :int
226228
attach_function :seekdir, [:pointer, :long], :void
227-
select_args = [:int, :pointer, :int, :pointer, :int, :pointer, :long]
228-
attach_function :truffleposix_select, select_args, :int, LIBTRUFFLEPOSIX
229229
attach_function :truffleposix_stat, [:string, :pointer], :int, LIBTRUFFLEPOSIX
230230
attach_function :truffleposix_stat_mode, [:string], :mode_t, LIBTRUFFLEPOSIX
231231
attach_function :truffleposix_stat_size, [:string], :long, LIBTRUFFLEPOSIX
@@ -240,8 +240,8 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
240240
Truffle::Boot.delay do
241241
if NATIVE
242242
# We should capture the non-lazy method
243-
attach_function_eagerly :truffleposix_select, select_args, :int, LIBTRUFFLEPOSIX, false, :truffleposix_select, self
244-
SELECT = method(:truffleposix_select)
243+
attach_function_eagerly :poll, [:pointer, :long, :int], :int, LIBC, false, :poll, self
244+
POLL = method(:poll)
245245
end
246246
end
247247

0 commit comments

Comments
 (0)