Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Manhole: wrap coroutines in defer.ensureDeferred automatically #10602

Merged
merged 3 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10602.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Synapse manhole no longer needs coroutines to be wrapped in `defer.ensureDeferred`.
2 changes: 1 addition & 1 deletion docs/manhole.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This gives a Python REPL in which `hs` gives access to the
`synapse.server.HomeServer` object - which in turn gives access to many other
parts of the process.

Note that any call which returns a coroutine will need to be wrapped in `ensureDeferred`.
Note that, prior to Synapse 1.41, any call which returns a coroutine will need to be wrapped in `ensureDeferred`.

As a simple example, retrieving an event from the database:

Expand Down
14 changes: 14 additions & 0 deletions synapse/util/manhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import sys
import traceback

Expand All @@ -20,6 +21,7 @@
from twisted.conch.manhole import ColoredManhole, ManholeInterpreter
from twisted.conch.ssh.keys import Key
from twisted.cred import checkers, portal
from twisted.internet import defer

PUBLIC_KEY = (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5"
Expand Down Expand Up @@ -141,3 +143,15 @@ def showtraceback(self):
self.write("".join(lines))
finally:
last_tb = ei = None

def displayhook(self, obj):
"""
We override the displayhook so that we automatically convert coroutines
into Deferreds. (Our superclass' displayhook will take care of the rest,
by displaying the Deferred if it's ready, or registering a callback
if it's not).
"""
if inspect.iscoroutine(obj):
super().displayhook(defer.ensureDeferred(obj))
else:
super().displayhook(obj)