Skip to content

Commit

Permalink
picoev: fix for windows apps with veb in a thread, parallel to a webv…
Browse files Browse the repository at this point in the history
…iew, that opens a lot of file descriptors (vlang#23492)
  • Loading branch information
spytheman authored Jan 16, 2025
1 parent d23e70f commit d5aa37d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions vlib/picoev/constants_default.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module picoev

// max_fds is the maximum number of file descriptors that can be managed.
// Many sizes depend on it, and some internal arrays are also iterated based on it,
// so increasing it a lot can slow down looping :-| .
pub const max_fds = 1024
11 changes: 11 additions & 0 deletions vlib/picoev/constants_windows.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module picoev

// max_fds is the maximum number of file descriptors that can be managed.
// Many sizes depend on it, and some internal arrays are also iterated based on it,
// so increasing it a lot can slow down looping :-| .
// It is higher on windows, because if you start a veb/picoev webservice in a thread,
// the returned file descriptors can be higher than 1024 in value, especially if you
// also have a webview running in another thread, that also opens its own file descriptors.
// Note: this works, because on windows we use select, and select on win32,
// is not limited to polling on only 1024 fds.
pub const max_fds = 4096
8 changes: 3 additions & 5 deletions vlib/picoev/picoev.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import net
import picohttpparser
import time

// maximum number of file descriptors that can be managed
pub const max_fds = 1024

// maximum size of the event queue
pub const max_queue = 4096

Expand Down Expand Up @@ -70,12 +67,12 @@ pub struct Picoev {
max_write int = 8192
mut:
loop &LoopType = unsafe { nil }
file_descriptors [max_fds]&Target
file_descriptors [4096]&Target // TODO: use max_fds here, instead of the hardcoded size, when the compiler allows it
timeouts map[int]i64
num_loops int

buf &u8 = unsafe { nil }
idx [1024]int
idx [max_fds]int
out &u8 = unsafe { nil }

date string
Expand Down Expand Up @@ -192,6 +189,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
}
if accepted_fd >= max_fds {
// should never happen
elog('Error during accept, accepted_fd >= max_fd')
close_socket(accepted_fd)
return
}
Expand Down

0 comments on commit d5aa37d

Please sign in to comment.