google / google/gvisor

mmap never returns ENOMEM / python fails to raise MemoryError

Open
#2,534 4 comments 0 reactions 0 assignees View on GitHub
area: compatibility area: stability priority: p2 type: enhancement
Dominant language
Go
Stars
19.3k
Forks
2k
Avg merge
3d 5h
Merged PRs (30d)
264

Description

Followup from https://github.com/google/gvisor/issues/2520

First configure host to simplify the problem:
```
swapoff -a
sysctl -w vm.overcommit_ratio=0
```

Then, run within a container:
```
root@runsc:~# python3
>>> a = 'a' * 1024*1024*1024*1024
```

What happens is the container dies due to OOM, see https://github.com/google/gvisor/issues/2533

This ticket is specifically about `mmap` semantics. On host, on normal system python reacts with MemoryError:
```
>>> a = 'a' * 1024*1024*1024*20000
Traceback (most recent call last):
File "", line 1, in
MemoryError
```

under the hood mmap fails for python
```
strace: Process 16648 attached
mmap(NULL, 1073745920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f180e30b000
mmap(NULL, 21474836484096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
mmap(NULL, 21474836615168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
```
This causes python to raise MemoryError.

I don't know what speciifcally gvisor does, but it seems either it doesn't do similar mmap with ENOMEM, or it doesn't propagate the error. I would expect host ENOMEM to be propagated.

The runsc --debug --strace shows successful mmap
```
I0424 10:57:18.938548 18306 strace.go:581] [ 2] python3 E munmap(0x7f65b831d000, 0x101000)
I0424 10:57:18.938834 18306 strace.go:619] [ 2] python3 X munmap(0x7f65b831d000, 0x101000) = 0x0 (226.021µs)
I0424 10:57:18.939010 18306 strace.go:593] [ 2] python3 E mmap(0x0, 0x10000001000, 0x3, 0x22, 0xffffffffffffffff (bad FD), 0x0)
I0424 10:57:18.939058 18306 strace.go:631] [ 2] python3 X mmap(0x0, 0x10000001000, 0x3, 0x22, 0xffffffffffffffff (bad FD), 0x0) = 0x7e02ca400000 (15.052µs)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.