Commit Graph

612 Commits

Author SHA1 Message Date
Andreas Kling
9ad9c65368 Revert "LibCore: Add thread-safe weak deferred_invoke()"
This reverts commit 96ce468b60.

Appears to have regressed WPT.
2026-01-25 12:20:39 +01:00
Andreas Kling
96ce468b60 LibCore: Add thread-safe weak deferred_invoke()
Add a thread-safe deferred_invoke() API on WeakEventLoopReference that
queues work onto the owning thread's event queue and wakes that thread
via EventLoopManager hooks. This avoids calling wake() from foreign
threads during teardown.

Implement current_thread_handle()/wake_thread() in each backend and
track per-thread data so handles are validated before waking:

- Unix: wake via per-thread wake pipe
- Windows: wake via thread wake event
- macOS: wake via stored CFRunLoopRef
- Qt: wake via event target or QEventLoop::wakeUp()
- Android: wake via stored ALooper
2026-01-25 09:32:51 +01:00
Andreas Kling
2734f72835 LibCore: Fix data race in WeakEventLoopReference::revoke()
This was taking a read lock while modifying m_event_loop, which is a
data race with concurrent calls to take() that also hold read locks.
2026-01-25 09:32:51 +01:00
Andreas Kling
2a6045833c LibCore: Make ProxyData::port a u16
TCP ports are always u16 and this prevents invalid values from getting
into this field somehow.
2026-01-22 17:38:15 +01:00
Zaggy1024
4a5350a2ee LibCore: Use sendfile instead of mmap+write on macOS 2026-01-19 06:53:29 -05:00
Zaggy1024
84c0eb3dbf LibCore+LibHTTP+RequestServer: Send data via sockets instead of pipes
This brings the implementation on Unix in line with Windows, so we can
drop a few ifdefs.
2026-01-19 06:53:29 -05:00
Zaggy1024
a3f0b513b6 LibCore: Remove an unnecessary ifdef from ioctl
Passing argp to ioctl as void* instead of FlatPtr works on Linux, but
Haiku needs a void*, so let's just do that unconditionally.
2026-01-19 06:53:29 -05:00
Zaggy1024
8eda26c5cf LibCore+Everywhere: Make Windows's System::ioctl consistent with POSIX
Passing the option by value on Windows where it's a pointer on all
other platforms seems like it may cause some unnecessary ifdef soup.
2026-01-19 06:53:29 -05:00
Undefine
822d8463c2 LibCore: Use the Linux implementation of TimeZoneWatcher on BSDs
The same implementation works just fine on any BSD system provided it
has a working FileWatcher implementation.
2026-01-16 10:59:50 -07:00
Undefine
941797c807 LibCore: Implement FileWatcher for any systems with inotify
FreeBSD 15 added support for inotify so let's just allow this file to
be used there too as it's needed for watching the time zone file.
2026-01-16 10:59:50 -07:00
Jelle Raaijmakers
771a9c9266 LibCore: Simplify anon_create()
After 07630f3b0c, this simplification
became obvious. No functional changes.
2026-01-16 07:26:46 -05:00
Andreas Kling
07630f3b0c LibCore: Fix memory leak in anon_create() on BSD/macOS
The mmap() call here served no purpose - anon_create() is only meant to
create and return a file descriptor. The actual mapping is done later
by AnonymousBufferImpl::create().

This leaked an mmap of `size` bytes for every AnonymousBuffer created,
which includes backing stores, shareable bitmaps, and more.
2026-01-16 08:04:22 +01:00
Andreas Kling
0e6f2cb734 LibCore: Add pipe2() implementation for Windows
This is needed by LibWebView's process output capture feature.
2026-01-13 21:05:58 +01:00
Andreas Kling
568758e55a LibCore: Fix Windows build for STDOUT_FILENO/STDERR_FILENO macros
Wrap the _get_osfhandle() return value with to_fd() to convert from
intptr_t to int, fixing a narrowing conversion error on Windows.
2026-01-13 21:05:58 +01:00
Tim Ledbetter
2a58b5b608 Revert "LibCore: Use weak ownership in EventReceiver::deferred_invoke"
This reverts commit 1ed94388e9.
2026-01-11 13:54:53 -05:00
ayeteadoe
1ed94388e9 LibCore: Use weak ownership in EventReceiver::deferred_invoke
When test-web has completed running all tests there is a pending
DeferredInvoke in the main threads event queue. For Unix pthread, the
main threads event queue is leaked as the destructor callback in
pthread_key_create is not invoked. For Windows pthreads4w, the
destructor callback is invoked when the main thread is exiting. When
the main threads event queue is destroyed, the pending DeferredInvoke
event is destroyed which causes a leftover WebContentClient reference
to also get destroyed; however, the static WebContentClient::s_clients
HashTable has already been destroyed at this point, so we get a UAF in
the WebContentClient destructor and ASAN reports that error.

The reason why cleaning up the pending deferred invoke results in a
WebContentClient instance also being cleaned up is that class inherits
from IPC::ConnectionBase which is a Core::EventReceiver. The
deferred_invoke() method exposed on event receivers takes a strong
reference to itself to ensure it is still alive by the time the event
loop is ready to execute the function. There are a couple places in
IPC::ConnectionBase::drain_messages_from_peer() that utilized deferred
invocation which is why we have a leftover WebContentClient that has
past its useful lifetime at the end of TestWeb::run_tests().

Instead of holding onto a strong reference when the event has not yet
been processed, we take a weak reference and only grab a strong ref if
the receiver is alive when the event loop is about to execute our
function.
2026-01-10 23:35:51 +01:00
Andreas Kling
3a0e2c3d0e LibCore: Notify write-only notifiers on POLLHUP
This fixes an issue where RequestServer would churn as poll() returned
immediately due to a file descriptor yielding POLLHUP.

In that case, we should just wake the notifier and let it figure out
what to do.
2026-01-10 14:58:21 -05:00
Zaggy1024
fb2d9e3c85 LibCore: Only wake the event loop in deferred_invoke from other threads
For some reason, writing to the wake fd from the same thread in
deferred_invoke was causing a deadlock. However, we don't actually need
to wake from the same thread, since the event loop is not waiting and
will therefore process the deferred_invoke on the next iteration.

This issue was introduced in 3742138cc3.
The deadlock could be reproduced consistently by increasing
LOCAL_STORAGE_QUOTA in StorageJar.cpp to a large value like 50 MiB.
2025-12-31 10:41:51 +01:00
Zaggy1024
87c3053370 LibCore: Allow checking for a ThreadEventQueue without creating one 2025-12-31 10:41:51 +01:00
Arran Ireland
bd82dfa048 AK+LibURL: Use AK::IPv4/6 in Host
This resolves two FIXME comments.
2025-12-31 10:24:56 +01:00
Zaggy1024
685535dc85 LibCore: Rename the forward declaration of WeakEventLoopReference
This was missed by the rename tool when changing EventLoopWeak to
WeakEventLoopReference.
2025-12-09 17:16:25 -06:00
Zaggy1024
3742138cc3 LibCore+LibThreading: Always wake the event loop when deferred invoking 2025-12-09 16:12:40 -06:00
Zaggy1024
2a5a9d2103 LibCore: Add a thread-safe weak link for EventLoop
Since the event loop has a very specifically scoped lifetime, we can't
ensure that it outlives threads that hold a reference to it without
blocking the thread that owns it. In order to make threads use the
event loop safely, we now have an atomically ref-counted
WeakEventLoopReference class that can be passed off to threads to
safely post events/callbacks to it.

Another possibility was to use an RWLock per event loop that each
thread holds a read lock on, while ~EventLoop() uses a write lock to
block and prevent it being destroyed until all its threads exit.
However, media data providers don't receive a signal to exit due to the
GC heap being intentionally leaked, so the process never actually
exits. It would be possible to specifically drop the reference to
PlaybackManager in HTMLMediaElement in order to make those data
providers die on their own, but that doesn't help prevent this problem
in other cases where it may arise.
2025-12-09 16:12:40 -06:00
Andreas Kling
05e449d32a LibCore: Simplify how we calculate processed event count 2025-12-03 13:26:27 +01:00
Andreas Kling
25764fbc54 LibCore: Remove debug logging about pending promise count
This wasn't really achieving anything other than occasionally confusing
people into thinking our JS promise system was broken.
2025-12-03 13:26:27 +01:00
Andreas Kling
cebc4d00dd LibCore: Remove ability to post heap-allocated Core::Event objects
We no longer need this API since all clients have been converted to
simply posting the Core::Event::Type (or a callback function).
2025-12-03 13:26:27 +01:00
Andreas Kling
2a1c5dc108 LibCore: Don't require heap-allocated event to hold deferred invocation
Instead of creating a DeferredInvocationEvent every time we
deferred_invoke, we now let the QueuedEvent store the invokee Function
object directly.
2025-12-03 13:26:27 +01:00
Andreas Kling
69515f8c85 LibCore: Allow posting stateless events without heap-allocated Event
This patch adds an API for posting a Core::Event::Type to the thread
event queue without requiring a full Core::Event object.

We use this API to avoid heap allocations for every timer and notifier
activation event.
2025-12-03 13:26:27 +01:00
Andreas Kling
23fb9781d1 LibCore: Avoid excessive ref-count churn in event dispatch
We were strongly reffing the event receiver twice before actually
invoking the event handlers.
2025-12-03 13:26:27 +01:00
Andreas Kling
fa85f62895 LibCore: Remove unused Core::Event::Type::Quit 2025-12-03 13:26:27 +01:00
Zaggy1024
b572ae95a9 LibCore+LibWeb: Remove the dummy EventReceiver from deferred_invoke()
The DeferredInvocationContext only existed to satisfy the requirement
in ThreadEventQueue that each event has an EventReceiver. However,
deferred_invoke() was not even using the EventReceiver to call its
callback. Therefore, we don't need to allocate one for every deferred
invocation.

This also prevents WeakPtr::strong_ref() from racing and leaking the
context object when invoking a function across threads.
2025-12-02 21:21:03 -06:00
Timothy Flynn
005e965380 LibCore+RequestServer: Return size_t from system read/write functions
The system uses ssize_t so it can return -1 in case of an error. But in
our case, we will transform that to an AK::Error, thus we never return
-1. Let's return size_t instead.
2025-12-01 16:29:48 +01:00
Timothy Flynn
edac716e91 LibCore: Support HTTP disk caching on Windows
This implements Core::System::transfer_file_through_pipe for Windows
(which is actually a socket, not a pipe). This let's us enable the
HTTP disk cache.
2025-12-01 08:55:33 -05:00
Timothy Flynn
079a2ba36a LibCore+RequestServer: Implement Core::System::send on Windows 2025-12-01 08:55:33 -05:00
Timothy Flynn
85fd4d5ea1 LibCore: Accept ReadonlyBytes/Bytes in System::send/recv
Instead of passing in a pointer+size, let's use safer AK types.
2025-12-01 08:55:33 -05:00
Timothy Flynn
90525f7e97 LibCore: Define SocketAddress out-of-line to remove SocketAddressWindows
SocketAddressWindows.h contains a bunch of copy-pasted Windows
definitions. This started causing ad-nauseam redefinition errors when
implementing the HTTP disk cache for Windows.

Instead, let's forward-declare the types we can in SocketAddress.h and
only define the couple of constants that we need. We can then assert at
compile-time that we defined them correctly.
2025-12-01 06:34:32 -05:00
Timothy Flynn
5c88c3718b LibCore: Do not include SocketAddressWindows.h in System.h
SocketAddressWindows.h contains (re)definitions of a bunch of system-
level types. This leads to a bunch of conflicts when trying to include
proper Windows headers in SystemWindows.cpp. This patch removes this
inclusion and just forward-declares the couple of types we need.
2025-12-01 06:34:32 -05:00
Timothy Flynn
2d22ef0db5 LibCore: Move AddressInfoVector to its own file
By defining this class entirely in the System.h header, we are relying
on ::freeaddrinfo being available. This has led to us polluting the
System.h header with system-level definitions on Windows by way of
SocketAddressWindows.h.
2025-12-01 06:34:32 -05:00
ayeteadoe
4fb1ba0193 LibCore: Remove unused NotifierActivationEvent fd() and type() methods
In 11b8bbe one thing that was claimed was that we now properly set the
Notifier's actual fd on the NotifierActivationEvent. It turns out that
claim was false because a crucial step was forgotten: actually set the
m_notifier_fd when registering. Despite that mistake, it ultimately was
irrelevant as the methods on NotifierActivationEvent are currently
unused code. We were posting the event to the correct Notifier receiver
so the on_activation was still getting invoked.

Given they are unused, NotifierActivationEvent can be defined the same
way as TimerEvent is, where we just pass the event type enum to the
Event base class. Additionally, NotificationType can be moved to
the Notifier header as this enum is now always used in the context of
creating or using a Notifier instance.
2025-11-22 09:47:25 +01:00
ayeteadoe
fa262d2db5 LibCore: Add VERIFY checks for all wait completion packet nt.dll calls 2025-11-18 18:49:37 +01:00
ayeteadoe
d5e5dbdf3d LibCore: Signal an event to queue a wake completion packet on Windows
The initial IOCP event loop implementation adjusted wake() to manually
queue a completion packet onto the current threads IOCP. This caused
us to now be dependent on the current threads IOCP, when the previous
behaviour did not depend on any data from the thread that was waking
the event loop.

Restoring that old behaviour allows https://hardwaretester.com/gamepad
to be loaded again.
2025-11-18 18:49:37 +01:00
ayeteadoe
540bbae480 LibCore: Restore single-shot timer objects manual reset on Windows
The initial IOCP event loop implementation removed the single shot
timer fix added in 0005207 was removed.

Adding this back allowed simple web pages like https://ladybird.org/ to
be loaded again.
2025-11-18 18:49:37 +01:00
ayeteadoe
11b8bbeadf LibCore: Use correct fd for NotifierActivationEvent on Windows
The initial IOCP event loop implementation had a fd() method for the
EventLoopNotifier packet that did not actually return the fd for the
notifier, but a to_fd() call on an object HANDLE that was always NULL.
This meant we were always posting NotifierActivationEvents with a fd of
0.

This rendered all of our WinSock2 I/O invalid, meaning no IPC messages
would ever be successfully sent or received.
2025-11-18 18:49:37 +01:00
Timothy Flynn
1c2c9a859a LibCore: Do not require rvalue references to resolve/reject promises
The way these methods were previously defined, we would have to `move`
values into these functions. This was pretty awkward for plain types.
For example, doing a `move` here really doesn't make sense:

    int resolution = 123;
    promise->resolve(move(resolution));

Let's declare these methods in a manner that allows callers to choose
whether values are moved or copied, using perfect forwarding.
2025-11-12 09:06:21 -05:00
Tim Ledbetter
1df94c4513 LibCore: Introduce REPORT_TIME() for per call timing information
This macro outputs the time taken to reach the end of the current scope
to the debug console. It also shows the average time, total cumulative
time and the total number of calls.

It is also possible to limit the amount of debug output using
`REPORT_TIME_EVERY(name, n)` to only print every `n` calls.
2025-11-11 11:47:59 +01:00
Undefine
6fa705c730 LibCore: Mark Process::wait_for_termination as const
After removal of the of the disown mechanism this can be marked as
const, which in my opinion makes sense for this API.
2025-11-07 11:27:51 +01:00
Undefine
769bbe6021 LibCore: Remove unused KeepAsChild and disown mechanisms
Those went unused and did nothing on systems other than SerenityOS.
2025-11-07 11:27:51 +01:00
Undefine
7a9dd46a39 LibCore: Remove unused Process::set_name
It went unused and was a no-op on systems other than SerenityOS.
2025-11-07 11:27:51 +01:00
Undefine
3c0d787c53 LibCore: Remove unused working_directory parameter
This was unused and a no-op on systems other than SerenityOS.
2025-11-07 11:27:51 +01:00
Undefine
41d04de1f2 LibCore: Remove unused System::uname
This went unused.
2025-11-07 11:27:51 +01:00