Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Chain writer failed due to TypeError #549

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

hoh
Copy link
Member

@hoh hoh commented Jan 31, 2024

The Chain writer task for Chain.ETH failed with the following stacktrace:

aleph-pyaleph-1 | 2024-01-29 14:09:32 [ERROR] aleph.chains.connector: Chain writer task for Chain.ETH failed, relaunching in 10 seconds.
aleph-pyaleph-1 | Traceback (most recent call last):
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/connector.py", line 63, in chain_writer_task
aleph-pyaleph-1 |     await connector.packer(config)
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/ethereum.py", line 351, in packer
aleph-pyaleph-1 |     await self.chain_data_service.prepare_sync_event_payload(
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/chain_data_service.py", line 75, in prepare_sync_event_payload
aleph-pyaleph-1 |     ipfs_cid = await self.storage_service.add_file(
aleph-pyaleph-1 |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/storage.py", line 293, in add_file
aleph-pyaleph-1 |     await self.add_file_content_to_local_storage(
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/storage.py", line 269, in add_file_content_to_local_storage
aleph-pyaleph-1 |     await self.storage_engine.write(filename=file_hash, content=file_content)
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/services/storage/fileystem_engine.py", line 26, in write
aleph-pyaleph-1 |     file_path.write_bytes(content)
aleph-pyaleph-1 |   File "/usr/lib/python3.11/pathlib.py", line 1066, in write_bytes
aleph-pyaleph-1 |     view = memoryview(data)
aleph-pyaleph-1 |            ^^^^^^^^^^^^^^^^
aleph-pyaleph-1 | TypeError: memoryview: a bytes-like object is required, not 'str'

It appears that a StringIO was passed to aleph.storage.add_file(...), resulting in file_content being of type str, where the underlying function aleph.services.storage.fileystem_engine.FileSystemStorageEngine.write expects the type bytes.

Solution:

  1. Encode the archive_content in UTF-8 before adding it in the storage service.
  2. Modify the type annotations accordingly
  3. Modify the signature of aleph.storage.StorageService.add_file to expect BytesIO instead of any IO type.

@hoh hoh force-pushed the hoh-fix-packing-string-in-memory branch from fa567ea to 19d2c51 Compare January 31, 2024 15:54
nesitor
nesitor previously approved these changes Jan 31, 2024
Solution: Use `bytes` in memory everywhere. Not elegant but fixes the problem.

---

The Chain writer task for Chain.ETH failed with the following stacktrace:

```
aleph-pyaleph-1 | 2024-01-29 14:09:32 [ERROR] aleph.chains.connector: Chain writer task for Chain.ETH failed, relaunching in 10 seconds.
aleph-pyaleph-1 | Traceback (most recent call last):
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/connector.py", line 63, in chain_writer_task
aleph-pyaleph-1 |     await connector.packer(config)
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/ethereum.py", line 351, in packer
aleph-pyaleph-1 |     await self.chain_data_service.prepare_sync_event_payload(
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/chains/chain_data_service.py", line 75, in prepare_sync_event_payload
aleph-pyaleph-1 |     ipfs_cid = await self.storage_service.add_file(
aleph-pyaleph-1 |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/storage.py", line 293, in add_file
aleph-pyaleph-1 |     await self.add_file_content_to_local_storage(
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/storage.py", line 269, in add_file_content_to_local_storage
aleph-pyaleph-1 |     await self.storage_engine.write(filename=file_hash, content=file_content)
aleph-pyaleph-1 |   File "/opt/pyaleph/src/aleph/services/storage/fileystem_engine.py", line 26, in write
aleph-pyaleph-1 |     file_path.write_bytes(content)
aleph-pyaleph-1 |   File "/usr/lib/python3.11/pathlib.py", line 1066, in write_bytes
aleph-pyaleph-1 |     view = memoryview(data)
aleph-pyaleph-1 |            ^^^^^^^^^^^^^^^^
aleph-pyaleph-1 | TypeError: memoryview: a bytes-like object is required, not 'str'
```

It appears that a `StringIO` was passed to `aleph.storage.add_file(...)`, resulting in `file_content` being of type `str`, where the underlying function `aleph.services.storage.fileystem_engine.FileSystemStorageEngine.write` expects the type `bytes`.

Solution:
1. Encode the `archive_content` in UTF-8 before adding it in the storage service.
2. Modify the type annotations accordingly
3. Modify the signature of `aleph.storage.StorageService.add_file` to expect `bytes` instead of any `IO` type.
@hoh hoh force-pushed the hoh-fix-packing-string-in-memory branch from eb1ce1f to 1213beb Compare February 14, 2024 09:10
Copy link
Member

@MHHukiewitz MHHukiewitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general, just a few questions for understanding everything correctly

src/aleph/web/controllers/ipfs.py Show resolved Hide resolved
src/aleph/web/controllers/ipfs.py Show resolved Hide resolved
@MHHukiewitz MHHukiewitz merged commit fde06f8 into main Feb 23, 2024
2 checks passed
@MHHukiewitz MHHukiewitz deleted the hoh-fix-packing-string-in-memory branch February 23, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants