From 5b1ecbde01591fef7928992adced759ba75249d8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Oct 2026 18:46:53 -0500 Subject: prevent event bus malfunction with Event type --- src/slskd/Events/EventBus.cs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/slskd/Events') diff --git a/src/slskd/Events/EventBus.cs b/src/slskd/Events/EventBus.cs index 0c9b8e57..72f797ff 100644 --- a/src/slskd/Events/EventBus.cs +++ b/src/slskd/Events/EventBus.cs @@ -88,6 +88,15 @@ public class EventBus public virtual void Raise(T data) where T : Event { + // we look up the list of subscribers using T, and then cast the callback to Func, and this breaks when + // T is Event rather than the derived type. the fix for this involves wrapping the callbacks with another delegate + // that performs the proper cast (or just rethinking this approach entirely), so we're just going to throw + // at run-time instead, which the smallest amount of functional testing will surface + if (typeof(T) == typeof(Event)) + { + throw new ArgumentException($"Raise can not be called with a type argument of type Event because the bus can't properly source the list of subscribers. Supply the derived Event Type when calling"); + } + Log.Debug("Handling {Type}: {Data}", typeof(T), data); // save the event to the database before broadcasting to consumers -- cgit v1.2.3 From 6e082e57998e1e3e72ef44c45a36b312ff903263 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Oct 2026 18:47:17 -0500 Subject: raise derived event types to avoid lookup issue --- src/slskd/Events/API/EventsController.cs | 49 +++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'src/slskd/Events') diff --git a/src/slskd/Events/API/EventsController.cs b/src/slskd/Events/API/EventsController.cs index e031498c..603d07d5 100644 --- a/src/slskd/Events/API/EventsController.cs +++ b/src/slskd/Events/API/EventsController.cs @@ -148,18 +148,45 @@ public class EventsController : ControllerBase { var d = disambiguator; - Event @event = eventType switch + Event @event; + + // each event must be raised using its concrete type so that the correct subscribers are invoked + switch (eventType) { - EventType.DownloadFileComplete => new DownloadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = new Transfer() }, - EventType.DownloadDirectoryComplete => new DownloadDirectoryCompleteEvent { LocalDirectoryName = $"{d}local.directory", RemoteDirectoryName = $"{d}remote.directory", Username = $"{d}username" }, - EventType.UploadFileComplete => new UploadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = new Transfer() }, - EventType.PrivateMessageReceived => new PrivateMessageReceivedEvent { Message = new PrivateMessage { Id = 42, Timestamp = DateTime.UtcNow, Username = $"{d}username", Message = $"{d}message" } }, - EventType.RoomMessageReceived => new RoomMessageReceivedEvent { Message = new RoomMessage { RoomName = $"{d}room", Username = $"{d}username", Message = $"{d}message" } }, - EventType.Noop => new NoopEvent(), - _ => throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."), - }; - - EventBus.Raise(@event); + case EventType.DownloadFileComplete: + var downloadFileComplete = new DownloadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = new Transfer() }; + EventBus.Raise(downloadFileComplete); + @event = downloadFileComplete; + break; + case EventType.DownloadDirectoryComplete: + var downloadDirectoryComplete = new DownloadDirectoryCompleteEvent { LocalDirectoryName = $"{d}local.directory", RemoteDirectoryName = $"{d}remote.directory", Username = $"{d}username" }; + EventBus.Raise(downloadDirectoryComplete); + @event = downloadDirectoryComplete; + break; + case EventType.UploadFileComplete: + var uploadFileComplete = new UploadFileCompleteEvent { LocalFilename = $"{d}local.file", RemoteFilename = $"{d}remote.file", Transfer = new Transfer() }; + EventBus.Raise(uploadFileComplete); + @event = uploadFileComplete; + break; + case EventType.PrivateMessageReceived: + var privateMessageReceived = new PrivateMessageReceivedEvent { Message = new PrivateMessage { Id = 42, Timestamp = DateTime.UtcNow, Username = $"{d}username", Message = $"{d}message" } }; + EventBus.Raise(privateMessageReceived); + @event = privateMessageReceived; + break; + case EventType.RoomMessageReceived: + var roomMessageReceived = new RoomMessageReceivedEvent { Message = new RoomMessage { RoomName = $"{d}room", Username = $"{d}username", Message = $"{d}message" } }; + EventBus.Raise(roomMessageReceived); + @event = roomMessageReceived; + break; + case EventType.Noop: + var noop = new NoopEvent(); + EventBus.Raise(noop); + @event = noop; + break; + default: + throw new SlskdException($"Event type {eventType} is an enum member but is not handled. Please submit an issue on GitHub."); + } + return StatusCode(201, @event); } catch (Exception ex) -- cgit v1.2.3