-
-
Notifications
You must be signed in to change notification settings - Fork 531
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
[Bug]: NPE when call DockerContainer.State() #540
Comments
@doubleknd26 I'm not able to reproduce this with the current tests. In #543 I'm directly calling the State func of the container, and I double checked that this new code, but applied on top of But looking at the code, the first call to // update container raw info
func (c *DockerContainer) inspectRawContainer(ctx context.Context) (*types.ContainerJSON, error) {
inspect, err := c.provider.client.ContainerInspect(ctx, c.ID)
if err != nil {
return nil, err
}
c.raw = &inspect
return c.raw, nil
}
// State returns container's running state
func (c *DockerContainer) State(ctx context.Context) (*types.ContainerState, error) {
inspect, err := c.inspectRawContainer(ctx)
if err != nil {
return c.raw.State, err
}
return inspect.State, nil
} I'm tracing back to the root cause, and noticed that the return of the raw value was changed in #271, from Could you share more about the tests where you found the panic? |
@mdelapenya Hi. It happened intermittently on our Github Action server managed by our company and unfortunately I cannot access the server. But, I'm sure the server doesn't have enough resource like cpu, mem. So I think It is because the container has been deleted unexpectedly. I tested on my local to check the case. Here is a sample test code. I hope it makes sense to you. |
Thanks for such detailed report! That seems correct: at the moment the container exits for any reasons (resource limits?) before the inspectRaw func is called, then any call to State should fail. I'm still in here:
It seems that returning |
@doubleknd26 I'm able to reproduce it with this simple test: func TestContainerStateAfterTermination(t *testing.T) {
ctx := context.Background()
nginxA, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{
nginxDefaultPort,
},
},
Started: true,
})
if err != nil {
t.Fatal(err)
}
err = nginxA.Terminate(ctx)
if err != nil {
t.Fatal(err)
}
state, err := nginxA.State(ctx)
assert.Nil(t, state, "expected nil container inspect.")
assert.Error(t, err, "expected error from container inspect.")
} The error comes because the State func is called after the container is terminated (probably something the user should know), but the library should be resilient enough to not panic and return a nil JSON state. I'm elaborating a fix at this moment. Thanks again for the report! |
Testcontainers version
0.13.0
Using the latest Testcontainers version?
Yes
Host OS
Linux
Host Arch
x86
Go Version
1.18
Docker version
Docker info
What happened?
When we call DockerContainer.State() in our test code, the NPE happened. IMHO, It seems because member variable raw is called without initialization and it is possible if inspectRawContainer, which initialize the variable 'raw', return error.
Relevant log output
Additional Information
No response
The text was updated successfully, but these errors were encountered: