Skip to content

Commit c2fd42e

Browse files
authored
fix Sockets type stability issues (#59404)
fixes #59397
1 parent d335458 commit c2fd42e

File tree

8 files changed

+86
-46
lines changed

8 files changed

+86
-46
lines changed

base/stream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ end
373373

374374
function isopen(x::Union{LibuvStream, LibuvServer})
375375
if x.status == StatusUninit || x.status == StatusInit || x.handle === C_NULL
376-
throw(ArgumentError("$x is not initialized"))
376+
throw(ArgumentError("stream not initialized"))
377377
end
378378
return x.status != StatusClosed
379379
end

stdlib/Sockets/src/PipeServer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function connect!(sock::PipeEndpoint, path::AbstractString)
103103
req = Libc.malloc(Base._sizeof_uv_connect)
104104
uv_req_set_data(req, C_NULL)
105105
ccall(:uv_pipe_connect, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}), req, sock.handle, path,
106-
@cfunction(uv_connectcb, Cvoid, (Ptr{Cvoid}, Cint)))
106+
@cfunction(uv_connectcb_pipe, Cvoid, (Ptr{Cvoid}, Cint)))
107107
sock.status = StatusConnecting
108108
iolock_end()
109109
return sock

stdlib/Sockets/src/Sockets.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ function send(sock::UDPSocket, ipaddr::IPAddr, port::Integer, msg)
456456
finally
457457
Base.sigatomic_end()
458458
iolock_begin()
459-
q = ct.queue; q === nothing || Base.list_deletefirst!(q::IntrusiveLinkedList{Task}, ct)
459+
q = ct.queue; q === nothing || Base.list_deletefirst!(q::Base.IntrusiveLinkedList{Task}, ct)
460460
if uv_req_data(uvw) != C_NULL
461461
# uvw is still alive,
462462
# so make sure we won't get spurious notifications later
@@ -474,9 +474,19 @@ end
474474

475475

476476
#from `connect`
477-
function uv_connectcb(conn::Ptr{Cvoid}, status::Cint)
477+
function uv_connectcb_tcp(conn::Ptr{Cvoid}, status::Cint)
478478
hand = ccall(:jl_uv_connect_handle, Ptr{Cvoid}, (Ptr{Cvoid},), conn)
479-
sock = @handle_as hand LibuvStream
479+
sock = @handle_as hand TCPSocket
480+
connectcb(conn, status, hand, sock)
481+
end
482+
483+
function uv_connectcb_pipe(conn::Ptr{Cvoid}, status::Cint)
484+
hand = ccall(:jl_uv_connect_handle, Ptr{Cvoid}, (Ptr{Cvoid},), conn)
485+
sock = @handle_as hand PipeEndpoint
486+
connectcb(conn, status, hand, sock)
487+
end
488+
489+
function connectcb(conn::Ptr{Cvoid}, status::Cint, hand::Ptr{Cvoid}, sock::LibuvStream)
480490
lock(sock.cond)
481491
try
482492
if status >= 0 # success
@@ -508,7 +518,7 @@ function connect!(sock::TCPSocket, host::Union{IPv4, IPv6}, port::Integer)
508518
end
509519
host_in = Ref(hton(host.host))
510520
uv_error("connect", ccall(:jl_tcp_connect, Int32, (Ptr{Cvoid}, Ptr{Cvoid}, UInt16, Ptr{Cvoid}, Cint),
511-
sock, host_in, hton(UInt16(port)), @cfunction(uv_connectcb, Cvoid, (Ptr{Cvoid}, Cint)),
521+
sock, host_in, hton(UInt16(port)), @cfunction(uv_connectcb_tcp, Cvoid, (Ptr{Cvoid}, Cint)),
512522
host isa IPv6))
513523
sock.status = StatusConnecting
514524
iolock_end()

stdlib/Sockets/src/addrinfo.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function getalladdrinfo(host::String)
9090
finally
9191
Base.sigatomic_end()
9292
iolock_begin()
93-
q = ct.queue; q === nothing || Base.list_deletefirst!(q::IntrusiveLinkedList{Task}, ct)
93+
q = ct.queue; q === nothing || Base.list_deletefirst!(q::Base.IntrusiveLinkedList{Task}, ct)
9494
if uv_req_data(req) != C_NULL
9595
# req is still alive,
9696
# so make sure we don't get spurious notifications later
@@ -223,7 +223,7 @@ function getnameinfo(address::Union{IPv4, IPv6})
223223
finally
224224
Base.sigatomic_end()
225225
iolock_begin()
226-
q = ct.queue; q === nothing || Base.list_deletefirst!(q::IntrusiveLinkedList{Task}, ct)
226+
q = ct.queue; q === nothing || Base.list_deletefirst!(q::Base.IntrusiveLinkedList{Task}, ct)
227227
if uv_req_data(req) != C_NULL
228228
# req is still alive,
229229
# so make sure we don't get spurious notifications later

test/trimming/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,32 @@ JULIAC_BUILDSCRIPT := $(shell $(JULIA) -e 'print(joinpath(Sys.BINDIR, Base.DATAR
3333

3434
#=============================================================================
3535

36-
release: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE)
36+
release: $(BIN)/hello$(EXE) $(BIN)/trimmability$(EXE) $(BIN)/basic_jll$(EXE)
3737

3838
$(BIN)/hello-o.a: $(SRCDIR)/hello.jl $(JULIAC_BUILDSCRIPT)
3939
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-exe true
4040

41+
$(BIN)/trimmability-o.a: $(SRCDIR)/trimmability.jl $(JULIAC_BUILDSCRIPT)
42+
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-exe true
43+
4144
$(BIN)/basic_jll-o.a: $(SRCDIR)/basic_jll.jl $(JULIAC_BUILDSCRIPT)
4245
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --project=$(SRCDIR) -e "using Pkg; Pkg.instantiate()"
4346
$(JULIA) -t 1 -J $(JULIA_LIBDIR)/julia/sys.$(SHLIB_EXT) --startup-file=no --history-file=no --project=$(SRCDIR) --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(JULIAC_BUILDSCRIPT) $< --output-exe true
4447

4548
$(BIN)/hello$(EXE): $(BIN)/hello-o.a
4649
$(CC) -o $@ $(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
4750

51+
$(BIN)/trimmability$(EXE): $(BIN)/trimmability-o.a
52+
$(CC) -o $@ $(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
53+
4854
$(BIN)/basic_jll$(EXE): $(BIN)/basic_jll-o.a
4955
$(CC) -o $@ $(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
5056

51-
check: $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE)
57+
check: $(BIN)/hello$(EXE) $(BIN)/trimmability$(EXE) $(BIN)/basic_jll$(EXE)
5258
$(JULIA) --depwarn=error $(SRCDIR)/trimming.jl $<
5359

5460
clean:
55-
-rm -f $(BIN)/hello$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/hello-o.a $(BIN)/basic_jll-o.a
61+
-rm -f $(BIN)/hello$(EXE) $(BIN)/trimmability$(EXE) $(BIN)/basic_jll$(EXE) $(BIN)/hello-o.a $(BIN)/trimmability-o.a $(BIN)/basic_jll-o.a
5662

5763
.PHONY: release clean check
5864

test/trimming/hello.jl

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,6 @@
1-
world::String = "world!"
2-
const str = OncePerProcess{String}() do
3-
return "Hello, " * world
4-
end
5-
6-
abstract type Shape end
7-
struct Square <: Shape
8-
side::Float64
9-
end
10-
struct Circle <: Shape
11-
radius::Float64
12-
end
13-
area(s::Square) = s.side^2
14-
area(c::Circle) = pi*c.radius^2
15-
16-
sum_areas(v::Vector{Shape}) = sum(area, v)
1+
# Test that minimal executable size stays low
172

183
function @main(args::Vector{String})::Cint
19-
println(Core.stdout, str())
20-
println(Core.stdout, PROGRAM_FILE)
21-
foreach(x->println(Core.stdout, x), args)
22-
23-
# test map/mapreduce; should work but relies on inlining and other optimizations
24-
# test that you can dispatch to some number of concrete cases
25-
println(Core.stdout, sum_areas(Shape[Circle(1), Square(2)]))
26-
27-
arr = rand(10)
28-
sorted_arr = sort(arr)
29-
tot = sum(sorted_arr)
30-
tot = prod(sorted_arr)
31-
a = any(x -> x > 0, sorted_arr)
32-
b = all(x -> x >= 0, sorted_arr)
33-
c = map(x -> x^2, sorted_arr)
34-
d = mapreduce(x -> x^2, +, sorted_arr)
35-
# e = reduce(xor, rand(Int, 10))
4+
println(Core.stdout, "Hello, world!")
365
return 0
376
end

test/trimming/trimmability.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Test that various constructs support trimming
2+
3+
using Sockets
4+
5+
world::String = "world!"
6+
const str = OncePerProcess{String}() do
7+
return "Hello, " * world
8+
end
9+
10+
abstract type Shape end
11+
struct Square <: Shape
12+
side::Float64
13+
end
14+
struct Circle <: Shape
15+
radius::Float64
16+
end
17+
area(s::Square) = s.side^2
18+
area(c::Circle) = pi*c.radius^2
19+
20+
sum_areas(v::Vector{Shape}) = sum(area, v)
21+
22+
function @main(args::Vector{String})::Cint
23+
println(Core.stdout, str())
24+
println(Core.stdout, PROGRAM_FILE)
25+
foreach(x->println(Core.stdout, x), args)
26+
27+
# test map/mapreduce; should work but relies on inlining and other optimizations
28+
# test that you can dispatch to some number of concrete cases
29+
println(Core.stdout, sum_areas(Shape[Circle(1), Square(2)]))
30+
31+
arr = rand(10)
32+
sorted_arr = sort(arr)
33+
tot = sum(sorted_arr)
34+
tot = prod(sorted_arr)
35+
a = any(x -> x > 0, sorted_arr)
36+
b = all(x -> x >= 0, sorted_arr)
37+
c = map(x -> x^2, sorted_arr)
38+
d = mapreduce(x -> x^2, +, sorted_arr)
39+
# e = reduce(xor, rand(Int, 10))
40+
41+
try
42+
sock = connect("localhost", 4900)
43+
if isopen(sock)
44+
write(sock, "Hello")
45+
flush(sock)
46+
close(sock)
47+
end
48+
catch
49+
end
50+
51+
return 0
52+
end

test/trimming/trimming.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ bindir = dirname(ARGS[1])
66
let exe_suffix = splitext(Base.julia_exename())[2]
77

88
hello_exe = joinpath(bindir, "hello" * exe_suffix)
9-
@test readchomp(`$hello_exe arg1 arg2`) == "Hello, world!\n$hello_exe\narg1\narg2\n$(4.0+pi)"
10-
@test filesize(hello_exe) < 2_500_000
9+
@test readchomp(`$hello_exe arg1 arg2`) == "Hello, world!"
10+
@test filesize(hello_exe) < 1_900_000
11+
12+
trimmability_exe = joinpath(bindir, "trimmability" * exe_suffix)
13+
@test readchomp(`$trimmability_exe arg1 arg2`) == "Hello, world!\n$trimmability_exe\narg1\narg2\n$(4.0+pi)"
1114

1215
basic_jll_exe = joinpath(bindir, "basic_jll" * exe_suffix)
1316
lines = split(readchomp(`$basic_jll_exe`), "\n")

0 commit comments

Comments
 (0)