Repository: matrix-org/synapse

Issue:
To-device messages delayed by `_DestinationWakeupQueue`
### Description

https://github.com/matrix-org/synapse/pull/12132 introduced functionality to spread out the federation transmission requests due to device-list updates. However it also applies to to-device messages (due to the default `immediate=False` at https://github.com/matrix-org/synapse/pull/12132/files#diff-4db75239ae91da3be91b6ccacb4dc969be778e832da2976b9b8fd628f16e6b52R674)  which doesn't *appear* to have been the intention, and can cause significant delays in transmission of to-device messages.

### Steps to reproduce

- log in on a new device
- soon after, send an E2E message
- express sadness at how long the keys take to turn up.

### Homeserver

sw1v.org

### Synapse Version

{"server_version":"1.77.0 (b=HEAD,t=v1.77.0,3e90dfdd8)","python_version":"3.7.3"}

### Installation Method

Docker (matrixdotorg/synapse)

### Database

postgres

### Workers

Single process

### Platform

debian somethingsomething

### Configuration

```
experimental_features:
  msc3958_supress_edit_notifs: True
```


### Relevant log output

Adjacent federation transmissions to `element.io`:

```shell

2023-02-23 13:31:47,814 - synapse.federation.sender.transaction_manager - 127 - INFO - federation_transaction_transmission_loop-97225 - TX [element.io] {1676822929244} Sending transaction [1676822929244], (PDUs: 1, EDUs: 2)
2023-02-23 13:31:48,277 - synapse.http.matrixfederationclient - 308 - INFO - federation_transaction_transmission_loop-97225 - {PUT-O-2230172} [element.io] Completed request: 200 OK in 0.46 secs, got 60 bytes - PUT matrix://element.io/_matrix/federation/v1/send/1676822929244
2023-02-23 13:31:48,277 - synapse.federation.sender.transaction_manager - 174 - INFO - federation_transaction_transmission_loop-97225 - TX [element.io] {1676822929244} got 200 response
2023-02-23 13:36:22,721 - synapse.federation.sender.transaction_manager - 127 - INFO - federation_transaction_transmission_loop-101748 - TX [element.io] {1676822931004} Sending transaction [1676822931004], (PDUs: 0, EDUs: 94)
2023-02-23 13:36:26,499 - synapse.http.matrixfederationclient - 308 - INFO - federation_transaction_transmission_loop-101748 - {PUT-O-2233711} [element.io] Completed request: 200 OK in 3.78 secs, got 11 bytes - PUT matrix://element.io/_matrix/federation/v1/send/1676822931004
2023-02-23 13:36:26,499 - synapse.federation.sender.transaction_manager - 174 - INFO - federation_transaction_transmission_loop-101748 - TX [element.io] {1676822931004} got 200 response
```


### Anything else that would be useful to know?

_No response_


Discussion:
see https://github.com/matrix-org/element-web-rageshakes/issues/20273 for an example of this
Could this be resolved by adding `immediate=True` to this call [here](https://github.com/matrix-org/synapse/blob/189a878a355f0c3933b24c7d7a4d22b11400f7f0/synapse/handlers/devicemessage.py#L303)?
> Could this be resolved by adding `immediate=True` to this call [here](https://github.com/matrix-org/synapse/blob/189a878a355f0c3933b24c7d7a4d22b11400f7f0/synapse/handlers/devicemessage.py#L303)?

I think that would fix this particular case, yes. But it looks to me as though the intention was that the *default* be `immediate=True`. @erikjohnston any opinions?
Ah, yup. I very much thing I meant the default should be `immediate=True`
Right I can fix that then, will put up a PR.

Resolving patch:
diff --git a/synapse/federation/send_queue.py b/synapse/federation/send_queue.py
--- a/synapse/federation/send_queue.py
+++ b/synapse/federation/send_queue.py
@@ -244,7 +244,7 @@ def send_presence_to_destinations(
 
         self.notifier.on_new_replication_data()
 
-    def send_device_messages(self, destination: str, immediate: bool = False) -> None:
+    def send_device_messages(self, destination: str, immediate: bool = True) -> None:
         """As per FederationSender"""
         # We don't need to replicate this as it gets sent down a different
         # stream.
diff --git a/synapse/federation/sender/__init__.py b/synapse/federation/sender/__init__.py
--- a/synapse/federation/sender/__init__.py
+++ b/synapse/federation/sender/__init__.py
@@ -783,7 +783,7 @@ def send_edu(self, edu: Edu, key: Optional[Hashable]) -> None:
         else:
             queue.send_edu(edu)
 
-    def send_device_messages(self, destination: str, immediate: bool = False) -> None:
+    def send_device_messages(self, destination: str, immediate: bool = True) -> None:
         if destination == self.server_name:
             logger.warning("Not sending device update to ourselves")
             return


Test patch: