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

Add container filesystem API for sandboxes #2414

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open

Conversation

azliu0
Copy link
Contributor

@azliu0 azliu0 commented Oct 27, 2024

Describe your changes

Resolves WRK-414 by adding a new container filesystem API.

This API is designed to closely follow io.FileIO, supporting most of the major functions (open, read, readline, readlines, write, seek, flush, close) along with a few higher-level functions (delete_bytes, write_replace_bytes) to help make Sandboxes a bit easier to use.

All file modes (e.g. text, bytes, r/w/a/x, etc.) are supported, as well as most common errors/exceptions. If an unknown exception is thrown, then a modal.exception.FilesystemExecutionError is thrown instead.

Example usage:

app = modal.App.lookup("sandbox-fs", create_if_missing=True)
sb = modal.Sandbox.create(app=app)

with sb.open("test.txt", "w") as f:
  f.write("Hello World\n")

f = sb.open("test.txt", "rb")
print(f.read())
Backward/forward compatibility checks

Check these boxes or delete any item (or this section) if not relevant for this PR.

  • Client+Server: this change is compatible with old servers
  • Client forward compatibility: this change ensures client can accept data intended for later versions of itself

Note on protobuf: protobuf message changes in one place may have impact to
multiple entities (client, server, worker, database). See points above.


Changelog

Sandboxes now support a new filesystem API. The open() method returns a FileIO handle for native file handling in sandboxes.

app = modal.App.lookup("sandbox-fs", create_if_missing=True)
sb = modal.Sandbox.create(app=app)

with sb.open("test.txt", "w") as f:
  f.write("Hello World\n")

f = sb.open("test.txt", "rb")
print(f.read())

@azliu0 azliu0 requested a review from pawalt October 28, 2024 12:27
@azliu0 azliu0 changed the title Add container filesystem for sandboxes Add container filesystem API for sandboxes Oct 28, 2024
@pawalt
Copy link
Member

pawalt commented Oct 28, 2024

@azliu0 can you convert this to draft until we have worker & server support? Also this will need to be tested somehow. Need to mock the underlying RPCs.

@azliu0 azliu0 marked this pull request as draft October 28, 2024 14:06
Base automatically changed from azliu/sandbox-fs-proto to main October 28, 2024 14:36
@azliu0 azliu0 marked this pull request as ready for review October 29, 2024 21:49
@azliu0 azliu0 requested a review from mwaskom October 29, 2024 21:52
@mwaskom
Copy link
Contributor

mwaskom commented Oct 30, 2024

Can we improve the changelog a bit here?

seen_chars.add(char)

def _handle_error(self, error: api_pb2.SystemErrorMessage) -> None:
if error.error_code == api_pb2.SystemErrorCode.SYSTEM_ERROR_CODE_UNSPECIFIED:
Copy link
Member

Choose a reason for hiding this comment

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

Can you just make this a case in the error mapping

error_class = ERROR_MAPPING.get(error.error_code, FilesystemExecutionError)
raise error_class(error.error_message)

async def _consume_output(self, exec_id: str) -> AsyncIterator[Optional[bytes]]:
Copy link
Member

Choose a reason for hiding this comment

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

Implement retries here similar to how they're implemented in StreamReader.

lines = cast(str, data).split("\n")
return [line + "\n" for line in lines[:-1]] + ([lines[-1]] if lines[-1] else [])

async def write(self, data: Union[bytes, str]) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Can we check data size here so we fail before making the req?

self._closed = False
return self

async def read(self, n: Union[int, None] = None) -> Union[bytes, str]:
Copy link
Member

Choose a reason for hiding this comment

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

Check data size before making the req

@@ -425,6 +426,22 @@ async def exec(self, *cmds: str, pty_info: Optional[api_pb2.PTYInfo] = None):
)
return _ContainerProcess(resp.exec_id, self._client)

async def open(self, path: str, mode: str = "r") -> _FileIO:
Copy link
Member

Choose a reason for hiding this comment

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

Can we support the context manager api too?

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