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

About file descriptors in lib/wasi & their POSIX compliance #3315

Closed
corwin-of-amber opened this issue Nov 17, 2022 · 7 comments
Closed

About file descriptors in lib/wasi & their POSIX compliance #3315

corwin-of-amber opened this issue Nov 17, 2022 · 7 comments
Assignees
Labels
priority-medium Medium priority issue ❓ question I've a question! 🏚 stale Inactive issues or PR
Milestone

Comments

@corwin-of-amber
Copy link

Summary

Generally, I am trying to understand the relationship between Inode, InodeVal, and Fd. Perhaps this mechanism can be improved.

Additional details

First, some background: I am running ocamlc on top of Wasmer (in wasm32). One thing that I noticed is that it opens the same file multiple times. This is weird perhaps, but is acceptable POSIX behavior. In Wasmer, I find an fd_map which maps int -> Fd, which contains an Inode, which in turn is mapped through inodes.arena to an InodeVal. The latter has a Kind which in turn holds a handle: Option<Box<dyn VirtualFile>>. As much as I look at it, it seems that the Inode is determined from the path at path_open, and the arena is indexed by the inode.index, such that if I open the same file multiple times, I am bound to get the same Inode and therefore the same InodeVal. This is in line with my observation, which is (finally): if I open the same file twice, and then close one of the file handles, then the other file handle is also closed as a side effect.

What I am asking is then two things: (i) what is the motivation for so many levels of indirection from an int fd to a handle? (ii) would it make sense to have handles keyed by fd instead?

It also strikes as a bit strange that files in the vfs have a FileDescriptor (usize) and that e.g. mem_fs maintains a position cursor for a File in file nodes. Because the position is associated with the fd, and the entire model kind of breaks if a file has multiple active fd's.

@corwin-of-amber corwin-of-amber added the ❓ question I've a question! label Nov 17, 2022
@syrusakbary
Copy link
Member

Hey @corwin-of-amber , we added vfs as an after-thought to wasi.
Initially wasmer-wasi handled all the filesystem by itself. And then we added support of doing that through the virtual filesystem.

What I am asking is then two things: (i) what is the motivation for so many levels of indirection from an int fd to a handle? (ii) would it make sense to have handles keyed by fd instead?

I believe most of the complexity came from the reasons I described before. I'm sure a lot of this logic could be refactored and improved.
Right now we are in the midst of a big refactor, but once that settles in I think it might be a good idea to start rethinking on how to simplify those layers

@syrusakbary
Copy link
Member

It also strikes as a bit strange that files in the vfs have a FileDescriptor (usize) and that e.g. mem_fs maintains a position cursor for a File in file nodes. Because the position is associated with the fd, and the entire model kind of breaks if a file has multiple active fd's.

This might be a bug that we need to address

@corwin-of-amber
Copy link
Author

Hi @syrusakbary! Thanks for the incredibly swift reply. I am happy to learn that you are going to take that into consideration. I will stay tuned to see the changes that you are doing in this refactor.

@fschutt fschutt added the priority-medium Medium priority issue label Nov 22, 2022
@fschutt fschutt assigned theduke and unassigned Michael-F-Bryan and theduke Nov 22, 2022
@fschutt fschutt added this to the v3.1 milestone Nov 22, 2022
@fschutt
Copy link
Contributor

fschutt commented Nov 22, 2022

Thank you for reporting, we are likely to do this after the WASIX merge, the filesystem code is currently frozen.

Yes, the Fd should either have a proper reference count and we need to make sure that the closing one Fd doesn't affect others, this is a serious bug.

The reason for the indirection is probably so that the Fd can be expanded later, which would not be possible with a simple integer.

@syrusakbary
Copy link
Member

This requires a bit of refactor on wasi and the filesystem. Will post here the updates

Copy link

stale bot commented Apr 24, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the 🏚 stale Inactive issues or PR label Apr 24, 2024
Copy link

stale bot commented May 24, 2024

Feel free to reopen the issue if it has been closed by mistake.

@stale stale bot closed this as completed May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-medium Medium priority issue ❓ question I've a question! 🏚 stale Inactive issues or PR
Projects
None yet
Development

No branches or pull requests

5 participants