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: patch-diff.githubusercontent.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:8b5a671b-614d-368c-564d-9fe62c2c1725 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C6A6:47A9E:2B1B40C:3BACFF0:69700CFA |
| html-safe-nonce | f72c10725da9826e895be606110e6b0cc62d9f47f52493559e4c9f8bc9db4d99 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkE2OjQ3QTlFOjJCMUI0MEM6M0JBQ0ZGMDo2OTcwMENGQSIsInZpc2l0b3JfaWQiOiI3MjkxMzQ1OTQ0NzE4NjA5NjU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f9e298077ca7c0a16934532b82189b887f60855dc9c7f2864b0e6e05af7d624a |
| 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 | 6e3493a220046ce21668e7217b5ac322bdc4edd2736ee04362e5462968204bd5 |
| 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 | 9f627dfc82200bff3d2b49e4fb4b0a99594a9a9a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width