Bug 4669 - HTTP keep-alive problem
Summary: HTTP keep-alive problem
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-26 11:57 UTC by Laszlo Dobos
Modified: 2012-04-27 15:57 UTC (History)
2 users (show)

Tags: http ssl keep-alive
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Laszlo Dobos 2012-04-26 11:57:58 UTC
When reading from a HTTP stream (i.e. not a document which has a well-defined size properly reported by the web server in the http response header), stream is empty after calling HttpWebRequest.GetResponseStream(). The problem arose when writing a client to capture twitter streams.

Here's the code to reproduce the bug. You need a valid twitter ID and password to test.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace TwitterStreamTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Mono requires this line to open SSL connections
            System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate
            {
                return true;
            };

            HttpWebRequest req = GetHttpWebRequest();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

            StreamReader stream = new StreamReader(res.GetResponseStream());

            char[] buffer = new char[20];

            while (stream.Read(buffer, 0, 20) > 0)
            {
                Console.WriteLine(buffer);
            }
        }

        static HttpWebRequest GetHttpWebRequest()
        {
            // Create and inizialize web request
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(@"https://stream.twitter.com/1/statuses/sample.json");
            req.Method = "GET";

            // Set common parameters
            req.AllowAutoRedirect = false;
            req.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            req.Credentials = new NetworkCredential("twitter_username", "twitter_password");


            // This works fine in Windows if set to true, stream is empty in mono,
            // so had to set to false to circumvent the bug
            req.KeepAlive = false;

            req.Pipelined = false;
            req.PreAuthenticate = false;
            req.UserAgent = "Twitter Streaming Client";

            return req;
        }
    }
}
Comment 1 Gonzalo Paniagua Javier 2012-04-27 10:53:06 UTC
All I see is an initial pause of a few seconds and then a steady stream of JSON data... Tried both master and mono-2-10 from the branch.

You don't mention which mono version you were using.
Comment 2 Laszlo Dobos 2012-04-27 14:46:51 UTC
When the line req.KeepAlive = false; changed to req.KeepAlive = true;

Works with this:

Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)


But not with this (compiled from source, based on git head, which is the newer!):

Mono JIT compiler version 2.11.2 (master/786db71 Thu Apr 26 16:53:42 CEST 2012)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)
Comment 3 Gonzalo Paniagua Javier 2012-04-27 15:46:42 UTC
Fixed in master and mono-2-10.

Thanks!
Comment 4 Gonzalo Paniagua Javier 2012-04-27 15:57:59 UTC
forgot to mention that it actually failed for me with current 2.10.x from the branch *and* master (which is where 2.11.2 came from).