Title: [BUG] promise mistreats pooled_function exceptions as done_callback exceptions · Issue #2542 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: [BUG] promise mistreats pooled_function exceptions as done_callback exceptions · Issue #2542 · python-telegram-bot/python-telegram-bot
X Title: [BUG] promise mistreats pooled_function exceptions as done_callback exceptions · Issue #2542 · python-telegram-bot/python-telegram-bot
Description: Steps to reproduce At the lowest level, create a telegram.ext.utils.Promise, throw an exception from the pooled_function on which the promise was built. Use DelayQueue using @mq.queuedmessage decorator on a subclassed Bot class. any exce...
Open Graph Description: Steps to reproduce At the lowest level, create a telegram.ext.utils.Promise, throw an exception from the pooled_function on which the promise was built. Use DelayQueue using @mq.queuedmessage decor...
X Description: Steps to reproduce At the lowest level, create a telegram.ext.utils.Promise, throw an exception from the pooled_function on which the promise was built. Use DelayQueue using @mq.queuedmessage decor...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/2542
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG] promise mistreats pooled_function exceptions as done_callback exceptions","articleBody":"\u003c!--\r\nThanks for reporting issues of python-telegram-bot!\r\n\r\nUse this template to notify us if you found a bug.\r\n\r\nTo make it easier for us to help you please enter detailed information below.\r\n\r\nPlease note, we only support the latest version of python-telegram-bot and\r\nmaster branch. Please make sure to upgrade & recreate the issue on the latest\r\nversion prior to opening an issue.\r\n-->\r\n### Steps to reproduce\r\n1. At the lowest level, create a `telegram.ext.utils.Promise`, throw an exception from the `pooled_function` on which the promise was built.\r\n2. Use `DelayQueue` using `@mq.queuedmessage` decorator on a subclassed Bot class. any exception like TimeOut or flood control within a `send_message` for example ends up with this problem\r\n3. Use MessageQueue - I know this is deprecated, but that isn't the problematic code here. Promise itself isn't correctly handling exceptions.\r\n\r\nIf you get any telegram exception, it just gets eaten up by Promise's run method in this specific area:\r\n```python\r\n if self._done_callback:\r\n try:\r\n self._done_callback(self.result())\r\n except Exception as exc:\r\n logger.warning(\r\n \"`done_callback` of a Promise raised the following exception.\"\r\n \" The exception won't be handled by error handlers.\"\r\n )\r\n logger.warning(\"Full traceback:\", exc_info=exc)\r\n```\r\n\r\nHere's the bug: even if there's an exception in `self.result()` it will treat it as an exception from `self._done_callback()` which isn't correct.\r\n\r\nThe funny thing is that code speaks out the problem on its own:\r\n```python\r\n try:\r\n self._result = self.pooled_function(*self.args, **self.kwargs)\r\n\r\n except Exception as exc:\r\n self._exception = exc\r\n```\r\nif there's an exception in `pooled_function`, it is stored in `self._exception`\r\n\r\nand then it is raised from `self.result()`:\r\n```python\r\n def result(self, timeout: float = None) -> Optional[RT]:\r\n self.done.wait(timeout=timeout)\r\n if self._exception is not None:\r\n raise self._exception # pylint: disable=raising-bad-type\r\n return self._result\r\n```\r\nwhy? this way all exception from `pooled_function` are forcibly going to be taken as exceptions of `done_callback`, eaten up by the promise code and never raised.\r\n\r\n### Expected behaviour\r\n`Promise` should ideally have another method called `add_exception_callback` or similar just like `add_done_callback`, and pass any exception from `pooled_function` to that callback\r\n\r\n### Actual behaviour\r\nPromise mistreats `pooled_function` exceptions as `done_callback` exceptions\r\n\r\n### Configuration\r\n**Operating System:**\r\nlinux\r\n\r\n**Version of Python, python-telegram-bot & dependencies:**\r\n```\r\nBot API 5.2\r\ncertifi 2020.12.05\r\nPython 3.8.5 (default, Jul 21 2020, 10:42:08) [Clang 11.0.0 (clang-1100.0.33.17)]\r\n```\r\n### Logs\r\nInsert logs here (if necessary)\r\n```\r\n2021-06-04 12:23:17:WARNING:DelayQueue-1:telegram.ext.utils.promise:91: `done_callback` of a Promise raised the following exception. The exception won't be handled by error handlers.\r\n2021-06-04 12:23:17:WARNING:DelayQueue-1:telegram.ext.utils.promise:95: Full traceback:\r\nTraceback (most recent call last):\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 402, in _make_request\r\n six.raise_from(e, None)\r\n File \"\u003cstring>\", line 2, in raise_from\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 398, in _make_request\r\n httplib_response = conn.getresponse()\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py\", line 1347, in getresponse\r\n response.begin()\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py\", line 307, in begin\r\n version, status, reason = self._read_status()\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py\", line 268, in _read_status\r\n line = str(self.fp.readline(_MAXLINE + 1), \"iso-8859-1\")\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py\", line 669, in readinto\r\n return self._sock.recv_into(b)\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py\", line 1241, in recv_into\r\n return self.read(nbytes, buffer)\r\n File \"/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py\", line 1099, in read\r\n return self._sslobj.read(len, buffer)\r\nsocket.timeout: The read operation timed out\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/utils/request.py\", line 253, in _request_wrapper\r\n resp = self._con_pool.request(*args, **kwargs)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py\", line 68, in request\r\n return self.request_encode_body(method, url, fields=fields,\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py\", line 148, in request_encode_body\r\n return self.urlopen(method, url, **extra_kw)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/poolmanager.py\", line 244, in urlopen\r\n response = conn.urlopen(method, u.request_uri, **kw)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 665, in urlopen\r\n retries = retries.increment(method, url, error=e, _pool=self,\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/util/retry.py\", line 347, in increment\r\n raise six.reraise(type(error), error, _stacktrace)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/packages/six.py\", line 686, in reraise\r\n raise value\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 614, in urlopen\r\n httplib_response = self._make_request(conn, method, url,\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 404, in _make_request\r\n self._raise_timeout(err=e, url=url, timeout_value=read_timeout,\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py\", line 321, in _raise_timeout\r\n raise exc_cls(*args)\r\ntelegram.vendor.ptb_urllib3.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=5.0)\r\n\r\nThe above exception was the direct cause of the following exception:\r\n\r\nTraceback (most recent call last):\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/ext/utils/promise.py\", line 89, in run\r\n self._done_callback(self.result())\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/ext/utils/promise.py\", line 116, in result\r\n raise self._exception # pylint: disable=raising-bad-type\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/ext/utils/promise.py\", line 80, in run\r\n self._result = self.pooled_function(*self.args, **self.kwargs)\r\n File \"/Users/asingh/workspace/tgbot/tgbotschedulebot/tg_mq_bot.py\", line 31, in editMessageText\r\n return super(MQBot, self).edit_message_text(*args, **kwargs)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/bot.py\", line 127, in decorator\r\n result = func(*args, **kwargs)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/bot.py\", line 2483, in edit_message_text\r\n return self._message(\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/bot.py\", line 296, in _message\r\n result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/bot.py\", line 259, in _post\r\n return self.request.post(\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/utils/request.py\", line 350, in post\r\n result = self._request_wrapper(\r\n File \"/Users/asingh/workspace/tgbot/venv/lib/python3.8/site-packages/telegram/utils/request.py\", line 255, in _request_wrapper\r\n raise TimedOut() from error\r\ntelegram.error.TimedOut: Timed out\r\n```","author":{"url":"https://github.com/angadsingh","@type":"Person","name":"angadsingh"},"datePublished":"2021-06-04T11:00:15Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/2542"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:248f8078-a6f0-811d-6387-6e4b9c4783bb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 834E:1F87:14265A0:191FF17:6B025F7B |
| html-safe-nonce | da80a686b45fc422a0c8d021b4a203f255834d7920864a63bb3ee0ebc3b7e917 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzRFOjFGODc6MTQyNjVBMDoxOTFGRjE3OjZCMDI1RjdCIiwidmlzaXRvcl9pZCI6Ijg2OTIwOTYxOTc3MjY0NjE4MTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b6efe9fe238e3696d2c9d85ca64589799f8c0efe6e35f8c0a92f3291f32653a5 |
| hovercard-subject-tag | issue:911386962 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/2542/issue_layout |
| twitter:image | https://opengraph.githubassets.com/82756c4758b90f6349a87a75c2aa96e391facc8802fb25c2950ef26d66eaf70f/python-telegram-bot/python-telegram-bot/issues/2542 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/82756c4758b90f6349a87a75c2aa96e391facc8802fb25c2950ef26d66eaf70f/python-telegram-bot/python-telegram-bot/issues/2542 |
| og:image:alt | Steps to reproduce At the lowest level, create a telegram.ext.utils.Promise, throw an exception from the pooled_function on which the promise was built. Use DelayQueue using @mq.queuedmessage decor... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | angadsingh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width