Skip to content
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

Closing incorrect source for new partial response without cache #43

Closed
liyzay opened this issue Dec 26, 2015 · 3 comments
Closed

Closing incorrect source for new partial response without cache #43

liyzay opened this issue Dec 26, 2015 · 3 comments
Labels
Milestone

Comments

@liyzay
Copy link

liyzay commented Dec 26, 2015

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.

@liyzay
Copy link
Author

liyzay commented Dec 26, 2015

Change the HttpProxyCache.responseWithoutCache() method to like this :

private void responseWithoutCache(OutputStream out, long offset) throws ProxyCacheException, IOException {
      HttpUrlSource newSource = new HttpUrlSource(this.source);
        try {
            newSource.open((int) offset);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int readBytes;
            while ((readBytes = newSource.read(buffer)) != -1) {
                out.write(buffer, 0, readBytes);
                offset += readBytes;
            }
            out.flush();
        } finally {
            newSource.close();
        }
    }

@danikula
Copy link
Owner

@liyzay you absolutely right. It is my fail 😬 .
Even more it seems this problem causes 29, 32, 37, 63, 66.

@danikula danikula changed the title A serious bug might cause MediaPlayer stop work Closing incorrect source for new partial response without cache Jul 29, 2016
@danikula danikula added the bug label Jul 29, 2016
@danikula danikula added this to the 2.5.0 milestone Jul 29, 2016
danikula added a commit that referenced this issue Jul 29, 2016
prevent invalid calling source.close() in different threads to avoid crashes on Lollipop (#37, #29, #63, #66)
danikula added a commit that referenced this issue Jul 29, 2016
)

prevent invalid calling source.close() in different threads to avoid crashes on Lollipop (#37, #29, #63, #66)
danikula added a commit that referenced this issue Jul 29, 2016
prevent invalid calling source.close() in different threads to avoid crashes on Lollipop (#37, #29, #63, #66)
@danikula
Copy link
Owner

Fixed here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants