Skip to content

Closing incorrect source for new partial response without cache #43

@liyzay

Description

@liyzay

This library use one HttpProxyCacheServerClients for same url ,for every single request,the HttpProxyCacheServer start a new thread to handle the request,so there would be more than one thread running with HttpProxyCacheServerClients.processRequest().
For partial request,it will go like this:
HttpProxyCacheServerClients.processRequest()->
HttpProxyCache.processRequest()->
HttpProxyCache.responseWithoutCache(),
here is the code of "responseWithoutCache()" method:

private void responseWithoutCache(OutputStream out, long offset) throws ProxyCacheException, IOException {
    try {
        HttpUrlSource source = new HttpUrlSource(this.source);
        source.open((int) offset);
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int readBytes;
        while ((readBytes = source.read(buffer)) != -1) {
            out.write(buffer, 0, readBytes);
            offset += readBytes;
        }
        out.flush();
    } finally {
       //this is where the bug happens,the source might being used in ProxyCache.readSource()**
       //if there is a another request running with HttpProxyCache.responseWithCache()**
       source.close();
    }
}

At the finally block,the "source.close()" will close the source might being used in ProxyCache.readSource() if there is a another request running with HttpProxyCache.responseWithCache(),so the cache thread will stop,then the MediaPlayer will get no data return.
I think what the author really want to do is to close the source in the try block.

PS:The log when the MediaPlayer stop work:

12-26 13:09:48.469 1759-1759/? I/ProxyCache: Proxy cache server started. Ping it...
12-26 13:09:48.479 1759-1800/? D/ProxyCache: Open connection  to http://127.0.0.1:37550/ping
12-26 13:09:48.479 1759-1799/? D/ProxyCache: Accept new socket Socket[address=/127.0.0.1,port=33754,localPort=37550]
12-26 13:09:48.479 1759-1801/? I/ProxyCache: Request to cache proxy:GetRequest{rangeOffset=0, partial=false, uri='ping'}
12-26 13:09:48.479 1759-1801/? D/ProxyCache: Opened connections: 0
12-26 13:09:48.489 1759-1800/? D/ProxyCache: Ping response: `ping ok`, pinged? true
12-26 13:09:48.649 1759-1799/? D/ProxyCache: Accept new socket Socket[address=/127.0.0.1,port=52510,localPort=37550]
12-26 13:09:48.649 1759-1799/? D/ProxyCache: Accept new socket Socket[address=/127.0.0.1,port=52511,localPort=37550]
12-26 13:09:48.649 1759-1799/? D/ProxyCache: Accept new socket Socket[address=/127.0.0.1,port=52512,localPort=37550]
12-26 13:09:48.649 1759-1799/? D/ProxyCache: Accept new socket Socket[address=/127.0.0.1,port=52513,localPort=37550]
12-26 13:09:48.649 1759-1806/? I/ProxyCache: Request to cache proxy:GetRequest{rangeOffset=0, partial=false, uri='http://video_url'}
12-26 13:09:48.679 1759-1806/? D/ProxyCache: Read content info from http://video_url
12-26 13:09:48.679 1759-1806/? D/ProxyCache: Open connection  to http://video_url
12-26 13:09:49.329 1759-1806/? I/ProxyCache: Content info for `http://video_url`: mime: video/mp4, content-length: 24051189
12-26 13:09:49.329 1759-1811/? D/ProxyCache: Open connection  to http://video_url
12-26 13:09:50.299 1759-1807/? I/ProxyCache: Request to cache proxy:GetRequest{rangeOffset=23737472, partial=true, uri='http://video_url'}
12-26 13:09:50.299 1759-1807/? D/ProxyCache: Open connection  with offset 23737472 to http://video_url
12-26 13:09:50.329 1759-1806/? D/ProxyCache: Closing socket… Socket is closed by client.
12-26 13:09:50.329 1759-1806/? D/ProxyCache: Releasing input stream… Socket is closed by client.
12-26 13:09:50.329 1759-1806/? D/ProxyCache: Opened connections: 1
12-26 13:09:51.969 1759-1807/? D/ProxyCache: Shutdown proxy for HttpUrlSource{url='http://video_url}
12-26 13:09:51.979 1759-1811/? E/ProxyCache: ProxyCache error
                                             com.danikula.videocache.ProxyCacheException: Error reading data from http://video_url
                                                 at com.danikula.videocache.HttpUrlSource.read(HttpUrlSource.java:99)
                                                 at com.danikula.videocache.ProxyCache.readSource(ProxyCache.java:126)
                                                 at com.danikula.videocache.ProxyCache.access$100(ProxyCache.java:19)
                                                 at com.danikula.videocache.ProxyCache$SourceReaderRunnable.run(ProxyCache.java:179)
                                                 at java.lang.Thread.run(Thread.java:856)
                                              Caused by: java.net.SocketException: Socket closed
                                                 at libcore.io.Posix.recvfromBytes(Native Method)
                                                 at libcore.io.Posix.recvfrom(Posix.java:131)
                                                 at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
                                                 at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
                                                 at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
                                                 at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
                                                 at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
                                                 at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
                                                 at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
                                                 at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
                                                 at com.danikula.videocache.HttpUrlSource.read(HttpUrlSource.java:95)
                                                 at com.danikula.videocache.ProxyCache.readSource(ProxyCache.java:126) 
                                                 at com.danikula.videocache.ProxyCache.access$100(ProxyCache.java:19) 
                                                 at com.danikula.videocache.ProxyCache$SourceReaderRunnable.run(ProxyCache.java:179) 
                                                 at java.lang.Thread.run(Thread.java:856) 
12-26 13:09:51.979 1759-1807/? D/ProxyCache: Opened connections: 0
12-26 13:09:58.659 1759-1808/? D/ProxyCache: Opened connections: 0
12-26 13:09:58.669 1759-1809/? D/ProxyCache: Opened connections: 0

PPS:I replaced the video url in the log.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions