Repository: matrix-org/synapse

Issue:
Trying to rejoin a local empty room gives a 500 error
In a quick setup of my custom server I tried the following and got an error:

- Create a new room
- Leave
- Try to join again
- Internal server error (M_UNKNOWN)

With both the web client and riot.im. But the room still exists, and I can see the history (only using riot.im, the synapse web client redirects to home).


Discussion:
@ChazyTheBest when I do this, I don't get an internal server error; I get a 404 with:
```
{"errcode":"M_UNKNOWN","error":"No known servers"}
```

This is somewhat expected, as per https://github.com/vector-im/riot-web/issues/1194, https://github.com/matrix-org/matrix-doc/issues/534, and https://github.com/matrix-org/synapse/issues/1533.

If you're actually seeing an internal server error, could you share logs from your homeserver?

> With both the web client and riot.im.

riot.im *is* the web client.
> > With both the web client and riot.im.

> riot.im is the web client.

ohh, you meant the matrix console client. Ignore the last message.
No, I meant the synapse web client, the one i can access via https://127.0.0.1:8448. The internal server error is from riot.im

Where are the logs located? I'm not too good with systemd and I think they may be in the journal somewhere.
The webapp bundled with synapse is called matrix console: http://matrix.org/docs/projects/client/matrix-console.html.

riot.im is just serving static assets; it's very unlikely to be serving an internal server error. Can you share a screenshot?

The synapse logs will be wherever you (or the distribution you use) has configured them to be... check the `homeserver.yaml` for `log_config` or `log_file`; or try `journalctl -u synapse` to get the logs from the `synapse` systemd unit. Sorry, it's very hard to guess where they might be. I suggest you come and get support in one of the public matrix rooms if you can't find them.
So that's it, sorry I didn't know.

Here's a screenshot of the error and the steps:

From riot.im:

- Create the new room
- Give the room a local address and list it publicly
- Check it's listed and enter again
- Leave room and try to rejoin

![screenshot_20170421_202505](https://cloud.githubusercontent.com/assets/654229/25291224/760afe7c-26d1-11e7-9b09-759d4d9b40c8.png)

The `homeserver.yaml` file says the log file is in `/var/lib/synapse/homeserver.log` but it's not there (not created).
Ok thanks. That looks like synapse is returning an ISE to the join request. 

I fear we're not going to get far without the synapse logs. 

Worth noting that `log_config` takes precedence over `log_file` if both are specified.
The `*log.config` file says the same: `filename: /var/lib/synapse/homeserver.log`.

I'll join the matrix room tomorrow to ask how to get the logs.
if you get the logs let us know and we can reopen this
I just ran into this Problem yesterday and want to share my log:
[matrix_synapse_exception.log](https://github.com/matrix-org/synapse/files/1810155/matrix_synapse_exception.log)

I used the docker image from Silvio to install a Synapse server and did just the same thing: 
- create a new room
- leave
- try to join again

If you need any further information let me know
@richvdh I have been able to reproduce this issue, using a different environment from what @david-austin has (details below).

### Description

Unable to join a room that contains no members. The homeserver returns HTTP 500 (internal server error) to the join request.

The room used to reproduce this belonged to the same homeserver to which I was connected.

### Steps to reproduce

1. Create a room (in this example, `#test-public:_redacted_.example.com`), make it joinable by all
2. Leave the room, meaning it has no members
3. Attempt to join the room again

- **Result**: server returns code 500.  
- **Client behaviour**:
  - riot-web shows a modal entitled "Failed to join room", with the message "Internal server error"
  - This [Weechat client](https://github.com/torhve/weechat-matrix-protocol-script) logs `HTTP API returned error. Code: 500, reason: Internal Server Error`


### `homeserver.log`

```
2018-04-08 22:29:40,829 - synapse.http.server - 191 - ERROR - POST-3- Failed handle request synapse.http.server._async_render on <synapse.rest.ClientRestResource object at 0x7f118ccd4fd0>: <XForwardedForRequest 
at 0x7f118c192b90 method=POST uri=/_matrix/client/r0/join/%23test-public%3A_redacted_.example.com?access_token=<redacted> clientproto=HTTP/1.0 site=8008>: Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1445, in unwindGenerator
    return _inlineCallbacks(None, gen, Deferred())
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = g.send(result)
  File "/usr/lib/python2.7/dist-packages/synapse/handlers/federation.py", line 1199, in _make_and_verify_event
    content,
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1445, in unwindGenerator
    return _inlineCallbacks(None, gen, Deferred())
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
    result = g.send(result)
  File "/usr/lib/python2.7/dist-packages/synapse/federation/federation_client.py", line 536, in make_membership_event
    raise RuntimeError("Failed to send to any server.")
exceptions.RuntimeError: Failed to send to any server.
```

### Environment information

- **Homeserver**: Own Synapse homeserver. Hostname is the same as Matrix 'server name', in both cases I've changed it to `_redacted_.example.com` here, as the homeserver is a private one.
- **Version**: 0.27.2
- **Install method**: apt, from Debian repo at https://matrix.org/packages/debian/
- **Platform**: Debian 9.4 Stretch, running in an Amazon EC2 t2.micro instance.
- **Networking**:
  - Port 8008 is proxied behind nginx on 443, but port 8448 goes directly to the Synapse daemon.
  - The request in this case came in on 443 and was forwarded by nginx to Synapse.
  - Both IPv4 and IPv6 in use.
This is https://github.com/matrix-org/matrix-doc/issues/534. Thanks for the comprehensive bug report, but the best bet is to upvote that one instead.
Thanks for the heads-up @ara4n; I was posting here mainly because of this:

> when I do this, I don't get an internal server error; I get a 404  
> If you're actually seeing an internal server error, could you share logs from your homeserver?

And I've reproduced this -- i.e. not getting 404 but 500, which seems to be a deviation from what's expected.
thanks for the reports. It looks like we return a 500 when the room alias is on the local server.

Resolving patch:
diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py
--- a/synapse/handlers/room_member.py
+++ b/synapse/handlers/room_member.py
@@ -852,6 +852,14 @@ def __init__(self, hs):
     def _remote_join(self, requester, remote_room_hosts, room_id, user, content):
         """Implements RoomMemberHandler._remote_join
         """
+        # filter ourselves out of remote_room_hosts: do_invite_join ignores it
+        # and if it is the only entry we'd like to return a 404 rather than a
+        # 500.
+
+        remote_room_hosts = [
+            host for host in remote_room_hosts if host != self.hs.hostname
+        ]
+
         if len(remote_room_hosts) == 0:
             raise SynapseError(404, "No known servers")
 


Test patch: