Skip to content

Memory leak on K8S #16183

Closed
Closed

Description

@simsicon
System information (version)
  • OpenCV => 4.1.0.25
  • Operating System / Platform => ubuntu 18.04 image on k8s (host: centos 7.6)
  • Docker version 19.03.4, build 9013bf583a
  • K8S v1.15.3
  • Compiler => installed with python pip
Detailed description

I have a tornado web service container using cv2.imdecode, and it causes memory leaking when I deployed the image to k8s, but it would not happen on the same machine with native docker rather than k8s.

I can reproduce the problem with the below code.

I used gdb info threads to find out that there were a lot of threads not collected:

(gdb) info threads
  Id   Target Id         Frame
* 1    Thread 0x7f78a8376580 (LWP 5991) "python" 0x00007f78a7c85603 in select () at ../sysdeps/unix/syscall-template.S:84
  2    Thread 0x7f789ed93700 (LWP 5992) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  3    Thread 0x7f789e592700 (LWP 5993) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  4    Thread 0x7f789dd91700 (LWP 5994) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  5    Thread 0x7f7899590700 (LWP 5995) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  6    Thread 0x7f7896d8f700 (LWP 5996) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  7    Thread 0x7f789458e700 (LWP 5997) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  8    Thread 0x7f7891d8d700 (LWP 5998) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  9    Thread 0x7f788f58c700 (LWP 5999) "python" pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
...more

when I tried to look into the thread, it seems my gdb is not well set:

(gdb) thread 5
[Switching to thread 5 (Thread 0x7fe775e0a700 (LWP 6052))]
#0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
185     ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S: No such file or directory.

The comparison I run with the native docker, it just display:

(gdb) info threads
No threads.

It proves that threads were not properly collected on k8s.

Steps to reproduce
import time
import cv2
import numpy as np

if __name__ == "__main__":
    with open("test.jpeg", "rb") as f:
        image = f.read()
        image_arr = np.fromstring(image, np.uint8)
        image_dec = cv2.imdecode(image_arr, cv2.IMREAD_COLOR)
        print(f"Image shape is {image_dec.shape}")

    while True:
        print("Sleep start")
        time.sleep(3)
        print("Sleep end")

Activity

  1. alalek commented on Dec 17, 2019

    @alalek
    Member
    1. Try to disable OpenCV threading OPENCV_FOR_THREADS_NUM=1.
    2. Avoid using of Python "multiprocessing". It uses fork() which breaks many parts of library (forked state is not consistent with broken synchronization primitives).
  2. mshabunin commented on Dec 17, 2019

    @mshabunin
    Contributor

    OpenCV creates thread pool and keeps it until the program is closed, it does not mean something is leaking. Number of threads can not be correctly detected in k8s or docker container, you can limit this number manually by calling cv2.setNumThreads() function.

    BTW, my docker container prints similar information (no k8s):

    (gdb) info threads
      Id   Target Id         Frame 
    * 1    Thread 0x7ffff7feb740 (LWP 4317) "python3" 0x00007ffff7afb03f in __GI___select (nfds=0, readfds=0x0, 
        writefds=0x0, exceptfds=0x0, timeout=0x7fffffffe140) at ../sysdeps/unix/sysv/linux/select.c:41
      2    Thread 0x7fffeb742700 (LWP 4321) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74eb60 <thread_status+96>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      3    Thread 0x7fffeaf41700 (LWP 4322) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ebe0 <thread_status+224>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      4    Thread 0x7fffe6740700 (LWP 4323) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ec60 <thread_status+352>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      5    Thread 0x7fffe3f3f700 (LWP 4324) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ece0 <thread_status+480>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      6    Thread 0x7fffe373e700 (LWP 4325) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ed60 <thread_status+608>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      7    Thread 0x7fffdef3d700 (LWP 4326) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ede0 <thread_status+736>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
      8    Thread 0x7fffdc73c700 (LWP 4327) "python3" 0x00007ffff77d29f3 in futex_wait_cancelable (
        private=<optimized out>, expected=0, futex_word=0x7fffed74ee60 <thread_status+864>)
        at ../sysdeps/unix/sysv/linux/futex-internal.h:88
    (gdb) 
    
  3. simsicon commented on Dec 18, 2019

    @simsicon
    Author

    Thanks for your information, I will try to test following your advises.

    However, I think it remains a thread pool with a limited threads is okay, but in my situation, the number of threads could go up to thousands on K8S after several minutes stress test, which cost a lot of memory and is not normal in my opinion.

  4. mshabunin commented on Dec 19, 2019

    @mshabunin
    Contributor

    Could you please provide this stress test?

  5. asmorkalov commented on Jan 31, 2020

    @asmorkalov
    Contributor

    Potentially related to the problem #16268

  6. ganesh-k13 commented on Feb 3, 2020

    @ganesh-k13
    Contributor

    @asmorkalov, there is a good chance it is related.

    Number of threads can not be correctly detected in k8s or docker container

    This is the issue being addressed in the issue mentioned above.

    Could you please provide this stress test?

    Can I add stress test for this? I have an on-prem Kubernetes test-setup so willing to spawn multiple threads. Any guidelines for adding them?

  7. asmorkalov commented on Feb 3, 2020

    @asmorkalov
    Contributor

    @ganesh-k13 It's good idea to test it, if you have K8s setup in your hands, but it's very hard to reproduce with OpenCV infrastructure and regular developer hosts. I do not think that portable test is possible here.

  8. ganesh-k13 commented on Feb 4, 2020

    @ganesh-k13
    Contributor

    Yeah, I'll just run in my local, with and without the changes and update here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions