Skip to content

Big file download from SMB via fast networks eats up memory #29328

Closed
owncloud-archive/documentation
#3503

Description

@mmattel

Steps to reproduce

  1. Have a big file (mine is 6.8GB) on an SMB share mounted to oC
  2. Having a (important!) fast connection between server and client (LAN)
  3. Download this file via browser

Expected behaviour

Download should pass without problems

Actual behaviour

Download stops after a while (3-4GB downloaded), system becomes unresponsive, after some minutes, system is accessible again.

This does not occur when

  • downloading small/smaller files via SMB
  • downloading from local, means same file is not on SMB but in the users oC home. In my environment the datadirectory is on NFS
  • downloading via a slow connection =eg internet, only a view mbit/s

Testing

Monitoring the system via htop under different scenarious
On fast/SMB, memory and swap gets eaten up, not from the beginning but after a while until system becomes unresponsive, download gets broken and recovers after a while beeing responsive againg and memory is recovered. Memory recovery also takes place if the file downloaded finishes earlier than memory becomes eaten up. This is monitorable.
libsmbclient installed in the latest public version (sudo pecl upgrade smbclient)
Network performance = browser (client) side, the SMB backend delivering the data is fast (55-60MB/s)

Slow Network, SMB: 3.2mbit/s (manually stopped after 510 MB = ~26min, no issues)
image

Fast Network, SMB 114mbit/s (memory starts raising from 345 to +603MB)
Memory raising appears between 1.2 and 3.7GB downloaded, mostly on higher numbers
image

Fast Network, SMB 114mbit/s (memory continues raising from 603 to +980MB)
Memory consumption continues
image

Pause or stop download at 980MB, waiting a minute
Memory gets freed up
image

Fast Network, Local (which is on an NFS mountpoint 200mbit/s (memory stays at ~380)
image

Server configuration

Operating system: ubuntu 16.04

Web server: nginx 1.13.6 (currently latest)

Database: mysql

PHP version: 7.0

ownCloud version: 10.0.3 production

Updated from an older ownCloud or fresh install: updated

Where did you install ownCloud from: tar

Signing status (ownCloud 9.0 and above):

No errors have been found.

The content of config/config.php:
config_report_20171023.txt

List of activated apps:

Enabled:
  - activity: 2.3.6
  - comments: 0.3.0
  - configreport: 0.1.1
  - dav: 0.3.0
  - federatedfilesharing: 0.3.1
  - federation: 0.1.0
  - files: 1.5.1
  - files_clipboard: 0.6.2
  - files_external: 0.7.1
  - files_external_dropbox: 1.0.0
  - files_external_ftp: 0.2.0
  - files_pdfviewer: 0.8.2
  - files_sharing: 0.10.1
  - files_texteditor: 2.2.1
  - files_trashbin: 0.9.1
  - files_versions: 1.3.0
  - files_videoplayer: 0.9.8
  - firstrunwizard: 1.1
  - gallery: 16.0.2
  - market: 0.2.2
  - music: 0.5.3
  - notifications: 0.3.1
  - provisioning_api: 0.5.0
  - systemtags: 0.3.0
  - templateeditor: 0.1
  - updatenotification: 0.2.1
Disabled:
  - encryption
  - external
  - files_antivirus
  - theme-example
  - user_external

Are you using external storage, if yes which one: local / smb / sftp /

Are you using encryption: no

Are you using an external user-backend, if yes which one: no

Client configuration

Browser: Opera, Chrome, same behaviour

Operating system: W10x64

Logs

Web server error log

nothing related

ownCloud log (data/owncloud.log)

nothing related, because I paused/stopped it. Else upstream timeout for php-fpm if I let it crash

Browser log

nothing related

@PVince81 @DeepDiver1975

Activity

  1. jvillafanez commented on Oct 23, 2017

    @jvillafanez
    Member

    Is libsmbclient-php installed and used?

  2. mmattel commented on Oct 23, 2017

    @mmattel
    ContributorAuthor

    yes, as written above

    dpkg -s libsmbclient
    
    Package: libsmbclient
    Status: install ok installed
    Priority: optional
    Section: libs
    Installed-Size: 276
    Maintainer: Ubuntu Developers <[email protected]>
    Architecture: amd64
    Multi-Arch: same
    Source: samba
    Version: 2:4.3.11+dfsg-0ubuntu0.16.04.11
    
  3. hodyroff commented on Oct 23, 2017

    @hodyroff

    More RAM solves the issue, or?

  4. mmattel commented on Oct 23, 2017

    @mmattel
    ContributorAuthor

    More memory just delays the time to crash and is not the solution.
    Low(er) memory systems for smaller environments suffer therefore most of it.
    These issues are necessary to be fixed with low mem systems so we do not run into issues with higher mem systems which are then much harder to investigate.
    I have 1.2GB of memory and the system fails to download a 6.8GB file ??
    Thinking about big backup or multimedia files ?

  5. mmattel commented on Oct 23, 2017

    @mmattel
    ContributorAuthor
    dpkg -s php-smbclient
    
    Package: php-smbclient
    Status: install ok installed
    Priority: optional
    Section: php
    Installed-Size: 84
    Maintainer: Ubuntu Developers <[email protected]>
    Architecture: amd64
    Version: 0.8.0~rc1-2build1
    Provides: php-libsmbclient
    Depends: ucf, php-common (>= 1:7.0+33~), phpapi-20151012, libc6 (>= 2.14), libsmbclient (>= 2:4.1.1)
    Conffiles:
     /etc/php/7.0/mods-available/smbclient.ini 854bee29f153a54b7e7cd275261d8073
    Description: PHP wrapper for libsmbclient
     smbclient is a PHP extension that uses Samba's libsmbclient library
     to provide Samba related functions and 'smb' streams to PHP programs.
    Original-Maintainer: Debian PHP PECL Maintainers <[email protected]>
    Homepage: http://pecl.php.net/smbclient
    
  6. added this to the triage milestone on Oct 23, 2017
  7. mmattel commented on Oct 23, 2017

    @mmattel
    ContributorAuthor

    phpini()

    Additional .ini files parsed
    /20-smbclient.ini, (shortened)
    libsmbclient .
    Version 0.9.0
    smbclient Support enabled
    smbclient extension Version 0.9.0
    libsmbclient library Version 4.3.11-Ubuntu
  8. mmattel commented on Oct 23, 2017

    @mmattel
    ContributorAuthor

    @kdslkdsaldsal refering #29328 (comment)
    even it has no impact to this case, pls adopt the documentation to reflect your statement.

  9. jvillafanez commented on Oct 24, 2017

    @jvillafanez
    Member

    @mmattel could you check running the following script from you nginx server? Adjust the parameters (workgroup, username, password and smbfile) in order to download your file.

    <?php
    $workgroup = null;
    $username = 'tester';
    $password = 'password';
    $smbfile = 'smb://localhost/testshare/testdir/testfile.txt';
    $localfile = '/tmp/localfile.txt';
    
    // Create new state:
    $state = smbclient_state_new();
    
    // Initialize the state with workgroup, username and password:
    smbclient_state_init($state, $workgroup, $username, $password);
    
    // Open a file for reading:
    $file = smbclient_open($state, $smbfile, 'r');
    
    $localfiledesc = fopen($localfile, 'wb');
    
    // Read the file incrementally, dump contents to output:
    while (true) {
    	$data = smbclient_read($state, $file, 4096);
    	if ($data === false || strlen($data) === 0) {
    		break;
    	}
    	fwrite($localfiledesc, $data);
    }
    
    fclose($localfiledesc);
    // Close the file handle:
    smbclient_close($state, $file);
    
    // Free the state:
    smbclient_state_free($state);
    

    I haven't seen any possible memory leak in our code, so it might be in the libsmbclient-php or lower libraries (libsmbclient), or maybe nginx could be buferring the whole response.

    The above script is quite simple and should help to determine where the problem might be:

    • If run standalone and the problem appears, then the memory leak should be either in the libsmbclient or libsmbclient-php libraries.
    • If run through nginx (supposing that the problem isn't in the libsmbclient nor libsmbclient-php libraries) and the problem appears, then nginx might be the one to blame.

    You can also check with apache. Last time I checked (with apache) in a VM with 2-3 GB of memory fetching a file of around 5 GB I didn't experience any issue. PHP memory limitations were the default (512 MB). This was between 2 different VM on the same host, although I didn't remember if I put some network constraint to test another issue.
    I'll recheck anyway.

  10. mmattel commented on Oct 24, 2017

    @mmattel
    ContributorAuthor

    (Edited)
    I tested as suggested, following results / comments:

    • I used the code above and adopted it a little to see if there are any errors.
      Copying a file from SMB to a local mount which is on NFS with 6.8 GB went fine.
      Memory consumtion was low and did not change. This means that there is no problem with libsmbclient-php or lower libraries without going thru the webserver
    • I would be great to test the same code idea but then output to the browser.
      I have no coding experience in that and need help.
    • Q: we read in the test made above the file via a while statement to chunk it until it´s end.
      The chunk result is continiously written with fwrite. When writing to the browser we use in our code readfile which sends from my understanding from a source. The question is, where is the data buffered?
      I reference https://zinoui.com/blog/download-large-files-with-php (chunked download) which uses echo and not readfile which is clear to me but not in how it seems we do it which looks to me as two seperate processes - or?
    • As written in the issue on top, it is a matter of low memory (my system has 1.2GB) AND SMB (local is fine) AND low/high bandwith to the browser where low bandwith causes no issues...

    My phi.ini has output_buffering = off and nginx is also configured with fastcgi_request_buffering off

  11. 9 remaining items

  12. mmattel commented on Oct 25, 2017

    @mmattel
    ContributorAuthor

    @kdslkdsaldsal
    I ran all tests not via cli but from the browser ! - forgot to mention...

    @jvillafanez
    I ran the test with the code adopted, it passes as fine, no memory issues. Which what I expected !
    But the difference looking into your statement is, that oC uses - as far I can identify it - readfile to pass the data to the browser while this tests use echo or fwrite inside a while block.

  13. jvillafanez commented on Oct 26, 2017

    @jvillafanez
    Member

    Yes, all the tests should be done through nginx because the problem is there.

    But the difference looking into your statement is, that oC uses - as far I can identify it - readfile to pass the data to the browser while this tests use echo or fwrite inside a while block.

    I don't think that's the case since all the file access should be done through the webdav interface, so downloads should be controlled by the webdav module, which is using stream_copy_to_stream function (or it should).
    The reason we aren't using that function is because we need an open stream to read the data from, which is more difficult to get without using ownCloud's code.

    You might want to check what happens if you add the following line in the apps/dav/lib/Connector/Sabre/FilesPlugin.php file (with the nginx buffering active):

                            $checksum = $node->getChecksum('sha1');
                            if ($checksum !== null && $checksum !== '') {
                                    $response->addHeader('OC-Checksum', $checksum);
                            }
                            $response->addHeader('X-Accel-Buffering', 'no');    // <----- line added
    

    That should disable the buffering for the file downloads

  14. mmattel commented on Oct 26, 2017

    @mmattel
    ContributorAuthor

    Just ran the test by commenting out fastcgi_buffering (defaults now to on)
    and adding the addHeader('X-Accel-Buffering', 'no') statement as suggested.
    I also tried it with: addHeader('X-Accel-Buffering: no');

    Memory consumption raises again.

    Personal opinion
    This is a very good approach, but maybe it was the wrong or not the only place to be added ...
    Maybe not all have the ability to change the nginx config because of hosting...

    Thinking about the following situation:
    Could that also become an issue on federated systems when loading multi GB files from one fed to the other in environments as described in the issue here??

  15. jvillafanez commented on Oct 31, 2017

    @jvillafanez
    Member

    I can confirm the problem with a similar configuration as the one proposed by ownCloud before removing the nginx buffering (data from "top")

     1503 www-data  20   0  564444  44692  33104 R 41.7  2.2   1:08.17 php-fpm7.0   
     1101 www-data  20   0  612764 491868   2940 S 32.3 24.0   0:51.64 nginx
    

    nginx memory consumption keeps increasing (php-fpm7.0 is stable)

    Adding the line said in #29328 (comment) seems to do the trick for me, keeping the nginx's memory usage low (0.2 comparing with the data above - php-fpm7.0 keeps a similar value of 2.2)

    I'm using the following configuration (based on what it is in the documentation):
    for the server configuration:

    upstream php-handler {
      server unix:/var/run/php/php7.0-fpm.sock;
    }
    
    server {
            listen 80;
            server_name localhost;
    
            # Path to the root of your installation
            root /opt/owncloud/;
    
            client_max_body_size 5G; # set max upload size
            fastcgi_buffers 64 4K;
    
            rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
            rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
            rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
    
            index index.php;
            error_page 403 /core/templates/403.php;
            error_page 404 /core/templates/404.php;
    
            location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
            }
    
            location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
                    deny all;
            }
    
            location / {
                    # The following 2 rules are only needed with webfinger
                    rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
                    rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
    
                    rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
                    rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
    
                    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    
                    try_files $uri $uri/ index.php;
            }
    
            location ~ ^(.+?\.php)(/.*)?$ {
                    try_files $1 = 404;
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$1;
                    fastcgi_param PATH_INFO $2;
                    fastcgi_pass php-handler;
            }
    
            # Optional: set long EXPIRES header on static assets
            location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
                    expires 30d;
                    # Optional: Don't log access to assets
                    access_log off;
            }
    }
    
    
    server {
      listen 443 ssl;
      server_name localhost;
    
      ssl_certificate /etc/nginx/ssl/server.crt;
      ssl_certificate_key /etc/nginx/ssl/server.key;
    
      # Path to the root of your installation
      root /opt/owncloud/;
      # set max upload size
      client_max_body_size 10G;
      fastcgi_buffers 64 4K;
    
      rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
      rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
      rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
    
      index index.php;
      error_page 403 /core/templates/403.php;
      error_page 404 /core/templates/404.php;
    
      location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
        }
    
      location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){
        deny all;
        }
    
      location / {
       # The following 2 rules are only needed with webfinger
       rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
       rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
    
       rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
       rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
    
       rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    
       try_files $uri $uri/ /index.php;
       }
    
       location ~ \.php(?:$|/) {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       fastcgi_param HTTPS on;
       fastcgi_pass php-handler;
       }
    
       # Optional: set long EXPIRES header on static assets
       location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
           expires 30d;
           # Optional: Don't log access to assets
             access_log off;
       }
    
      }
    

    nginx configuration is the default provided by ubuntu (mail section is commented, so it's ignored here):

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    
    events {
    	worker_connections 768;
    	# multi_accept on;
    }
    
    http {
    
    	##
    	# Basic Settings
    	##
    
    	sendfile on;
    	tcp_nopush on;
    	tcp_nodelay on;
    	keepalive_timeout 65;
    	types_hash_max_size 2048;
    	# server_tokens off;
    
    	# server_names_hash_bucket_size 64;
    	# server_name_in_redirect off;
    
    	include /etc/nginx/mime.types;
    	default_type application/octet-stream;
    
    	##
    	# SSL Settings
    	##
    
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    	ssl_prefer_server_ciphers on;
    
    	##
    	# Logging Settings
    	##
    
    	access_log /var/log/nginx/access.log;
    	error_log /var/log/nginx/error.log;
    
    	##
    	# Gzip Settings
    	##
    
    	gzip on;
    	gzip_disable "msie6";
    
    	# gzip_vary on;
    	# gzip_proxied any;
    	# gzip_comp_level 6;
    	# gzip_buffers 16 8k;
    	# gzip_http_version 1.1;
    	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
    	##
    	# Virtual Host Configs
    	##
    
    	include /etc/nginx/conf.d/*.conf;
    	include /etc/nginx/sites-enabled/*;
    }
    
  16. jvillafanez commented on Oct 31, 2017

    @jvillafanez
    Member

    @mmattel you might want to recheck the custom header solution (#29400). If that doesn't work, you might want to post your nginx configuration so we can check what setup is the problematic one.

  17. mmattel commented on Oct 31, 2017

    @mmattel
    ContributorAuthor

    Retried - works 👍

    image
    It seems that I have put the code in my test before into the wrong line...

  18. mmattel commented on Oct 31, 2017

    @mmattel
    ContributorAuthor
  19. mmattel commented on Nov 2, 2017

    @mmattel
    ContributorAuthor

    Update from nginx ticket:

    Are you using fastcgi_buffers 64 4K; as in the configuration provided in the issue? `
    If yes, please test with a lower number of buffers (at most 63, ​default 8 is fine).
    Using 64 fastcgi_buffers as in the configuration provided will end up in total 65 buffers 
    being used to read the response, and this makes it possible that ngx_readv_file() will 
    have to allocate memory which will be only freed when closing the connection, see d1bde5c3c5d2.
    

    And

    Yes buffers were set to fastcgi_buffers 64 4K;
    Testing with fastcgi_buffers 63 4K; as you said 63 should be max, downloading went fine 
    and no memory was eaten up.
    
    Two points:
    a.) The check in the changeset referenced is >64 and not >63 ...
    b.) Add a documentation note about this limit in ​
    http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffers 
    because many people do not know about this "silent" issue ... 
    

    Because we have in our documentation fastcgi_buffers 64 4K; as parameter set, I will do a doc PR asap to correct this (reducing to 8 4K respectively adding a note not to exeed 63).

    @jvillafanez we can add in the doc also the nginx conf parameter fastcgi_ignore_headers “X-Accel-Buffering” which would reenable buffering when set properly. If the admin can not set it, we are fine because of your PR to disable buffering in the header...

  20. jvillafanez commented on Nov 6, 2017

    @jvillafanez
    Member

    @jvillafanez we can add in the doc also the nginx conf parameter fastcgi_ignore_headers “X-Accel-Buffering” which would reenable buffering when set properly. If the admin can not set it, we are fine because of your PR to disable buffering in the header...

    Makes sense 👍

  21. removed this from the triage milestone on Apr 10, 2018
  22. lock commented on Jul 30, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  23. locked as resolved and limited conversation to collaborators on Jul 30, 2019
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions