Manhole: wrap coroutines in `defer.ensureDeferred` automatically (#10602)

pull/10620/head
reivilibre 2021-08-16 18:11:48 +01:00 committed by GitHub
parent 0db8cab72c
commit 19e51b14d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1 @@
The Synapse manhole no longer needs coroutines to be wrapped in `defer.ensureDeferred`.

View File

@ -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 `synapse.server.HomeServer` object - which in turn gives access to many other
parts of the process. 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: As a simple example, retrieving an event from the database:

View File

@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import inspect
import sys import sys
import traceback import traceback
@ -20,6 +21,7 @@ from twisted.conch.insults import insults
from twisted.conch.manhole import ColoredManhole, ManholeInterpreter from twisted.conch.manhole import ColoredManhole, ManholeInterpreter
from twisted.conch.ssh.keys import Key from twisted.conch.ssh.keys import Key
from twisted.cred import checkers, portal from twisted.cred import checkers, portal
from twisted.internet import defer
PUBLIC_KEY = ( PUBLIC_KEY = (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5" "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5"
@ -141,3 +143,15 @@ class SynapseManholeInterpreter(ManholeInterpreter):
self.write("".join(lines)) self.write("".join(lines))
finally: finally:
last_tb = ei = None 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)