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

Make ProcExit call Module.Close() #405

Closed
wants to merge 1 commit into from
Closed

Conversation

nullpo-head
Copy link
Contributor

This PR makes ProcExit call Module.Close() when it's called. proc_exit is expected to make the caller no longer usable, which means wazero can call Module.Close() and can release its resources. There may be further discussion that we can close the entire store, but closing module would be the good starting point anyway.

Signed-off-by: Takaya Saeki [email protected]

sys, err := newSysContext(nil, nil, nil)
require.NoError(t, err)

_, store, mod, fn := instantiateModule(t, context.Background(), FunctionProcExit, ImportProcExit, moduleName, sys)
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT, the signature change to return a 4th result is only used here. can you revert it and instantiate this test case different way? That prevents drifting almost all tests in this file.

Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

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

So, I would roll back changes to wasi_test.go.

There are two sorts of tests going on. wasi_test is unit tests and shouldn't do more than that. Anything more heavy should be tested in ad_hoc test so that multiple engines are used. testAdhocCloseWhileExecution does almost exactly what you were looking for and so heavier tests should be slight modifications to that.

For unit tests, there is little to do here, so should be a lot less code change. There's only one thing new going on. module.Close() is being invoked. You only need to test this was called, and that you are ignoring its errors (since the code added ignores errors).

So, for a test like that you need two instance of wasm.Module: one that succeeds on close and one that fails on close. You also need to be able to detect if it has closed.

You can instantiate the Module any way you like, including literally instantiating it. This means you can avoid heavy changes to the file to test these two cases.

Remember, WASI is only used when importing, so the importing module is the context used, you control which module instance is used when you use fn.Call

Hope these points help!


require.Nil(t, store.Module(moduleName)) // verify moduleName was removed because ProcExit calls Close
})
t.Run("close is called after proc_exit", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we prove we called Module.Close(), the this test is unnecessary unless we've not tested Module.Close() is safe to call twice. So, we are missing a test file module_context_test.go which can be added in another PR to say it is safe to close twice. We already test what it doesn't internally via TestStore_CloseModule (this tests twice is fine)

procExitFns := []struct {
name string
procExit func() (procExitFn, *wasm.Store)
}{
Copy link
Contributor

Choose a reason for hiding this comment

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

these tests should be in adhoc.go or equivalent so that they go against both engines. testAdhocCloseWhileExecution has some overlap with this, so maybe change the test that does this to use WASI? or perhaps add another test dimension to do the same with WASI also. In any case I would remove the import and back stuff as that's more about the engine.

"close_module": func() { _ = moduleCloser() }, // Closing while executing itself.

@nullpo-head
Copy link
Contributor Author

Your points totally make sense and I'm working on fixing. This PR is not closed but will be completely rebased after it.

@nullpo-head
Copy link
Contributor Author

Closing in favor of the different changes

@mathetake mathetake deleted the close_on_proc_exit branch March 24, 2022 02:05
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.

2 participants