Title: Chapter 21 - tcp_mojifinder.py crashes on empty search result · Issue #61 · fluentpython/example-code-2e · GitHub
Open Graph Title: Chapter 21 - tcp_mojifinder.py crashes on empty search result · Issue #61 · fluentpython/example-code-2e
X Title: Chapter 21 - tcp_mojifinder.py crashes on empty search result · Issue #61 · fluentpython/example-code-2e
Description: Issue If index.search(query) in tcp_mojifinder.py returns empty result, it crashes TCP because asyncio.StreamWriter.writelines(data) raises AssertionError: Data should not be empty error if data is an empty iterable. How to reproduce Sta...
Open Graph Description: Issue If index.search(query) in tcp_mojifinder.py returns empty result, it crashes TCP because asyncio.StreamWriter.writelines(data) raises AssertionError: Data should not be empty error if data is...
X Description: Issue If index.search(query) in tcp_mojifinder.py returns empty result, it crashes TCP because asyncio.StreamWriter.writelines(data) raises AssertionError: Data should not be empty error if data is...
Opengraph URL: https://github.com/fluentpython/example-code-2e/issues/61
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Chapter 21 - tcp_mojifinder.py crashes on empty search result","articleBody":"## Issue\n\nIf [`index.search(query)`](https://github.com/fluentpython/example-code-2e/blob/master/21-async/mojifinder/tcp_mojifinder.py#L45) in `tcp_mojifinder.py` returns empty result, it crashes TCP because [`asyncio.StreamWriter.writelines(data)`](https://docs.python.org/3/library/asyncio-stream.html#asyncio.StreamWriter.writelines) raises `AssertionError: Data should not be empty` error if `data` is an empty iterable.\n\n\n### How to reproduce\n\n\nStart the TCP server\n```shell\n% ./tcp_mojifinder.py \nBuilding index.\nServing on ('127.0.0.1', 2323). Hit CTRL-C to stop.\n...\n```\n\nConnect to the TCP server and send a search query which returns no results, e.g. `missing`\n```shell\n% nc localhost 2323\n?\u003e missing\n% \n```\n\nThe TCP server terminal will spit out an error stack trace like\n```shell\n% ./tcp_mojifinder.py \nBuilding index.\nServing on ('127.0.0.1', 2323). Hit CTRL-C to stop.\n From ('127.0.0.1', 63253): 'missing'\nUnhandled exception in client_connected_cb\ntransport: \u003c_SelectorSocketTransport fd=7 read=polling write=\u003cidle, bufsize=0\u003e\u003e\nTraceback (most recent call last):\n File \"/Users/tim/projects/python/example-code-2e/21-async/mojifinder/./tcp_mojifinder.py\", line 33, in finder\n results = await search(query, index, writer) # \u003c13\u003e\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/tim/projects/python/example-code-2e/21-async/mojifinder/./tcp_mojifinder.py\", line 48, in search\n writer.writelines(lines) # \u003c4\u003e\n ~~~~~~~~~~~~~~~~~^^^^^^^\n File \"/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/streams.py\", line 343, in writelines\n self._transport.writelines(data)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/selector_events.py\", line 1174, in writelines\n self._write_ready()\n ~~~~~~~~~~~~~~~~~^^\n File \"/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/selector_events.py\", line 1090, in _write_sendmsg\n assert self._buffer, 'Data should not be empty'\n ^^^^^^^^^^^^\nAssertionError: Data should not be empty\n```","author":{"url":"https://github.com/timheiko","@type":"Person","name":"timheiko"},"datePublished":"2025-08-08T16:06:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/61/example-code-2e/issues/61"}
| 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:cbd0f67a-db53-b71b-9850-1cfb87e7ac66 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E68:74018:29A68C8:3789BAC:6971CADB |
| html-safe-nonce | 3ad79bd453fd800988b3250f9563db263cc047a53502f536893a768d31dcd643 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTY4Ojc0MDE4OjI5QTY4Qzg6Mzc4OUJBQzo2OTcxQ0FEQiIsInZpc2l0b3JfaWQiOiI4NzUzMzExMzAxNDE4MjA3OTYzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a3e8c005afce442092fe827e6a24540b95e32453dbd55b289ee6fb2d886228d1 |
| hovercard-subject-tag | issue:3304530601 |
| 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/fluentpython/example-code-2e/61/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae751fdfacbeb0dd17f752b25ef8a57676c01bf2fe97608ad9e5d255c6f4b99c/fluentpython/example-code-2e/issues/61 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae751fdfacbeb0dd17f752b25ef8a57676c01bf2fe97608ad9e5d255c6f4b99c/fluentpython/example-code-2e/issues/61 |
| og:image:alt | Issue If index.search(query) in tcp_mojifinder.py returns empty result, it crashes TCP because asyncio.StreamWriter.writelines(data) raises AssertionError: Data should not be empty error if data is... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | timheiko |
| hostname | github.com |
| expected-hostname | github.com |
| None | ac615aa66802dad9a938c6abe95edf09ee43c0c7f508315b64b08612858ef32e |
| turbo-cache-control | no-preview |
| go-import | github.com/fluentpython/example-code-2e git https://github.com/fluentpython/example-code-2e.git |
| octolytics-dimension-user_id | 9216311 |
| octolytics-dimension-user_login | fluentpython |
| octolytics-dimension-repository_id | 176997413 |
| octolytics-dimension-repository_nwo | fluentpython/example-code-2e |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 176997413 |
| octolytics-dimension-repository_network_root_nwo | fluentpython/example-code-2e |
| 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 | 6b618569a5f93e0b31f97f620112341421ef5f69 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width