Bug 18814 - NullReferenceException when downloading files with WebClient.DownloadFile
Summary: NullReferenceException when downloading files with WebClient.DownloadFile
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Linux
: High major
Target Milestone: Untriaged
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2014-04-04 07:51 UTC by Marian Salaj
Modified: 2014-07-22 14:00 UTC (History)
8 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Christer Nordvik's test case modified for Linux (8.14 KB, application/zip)
2014-06-23 20:07 UTC, Narinder
Details


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 Marian Salaj 2014-04-04 07:51:17 UTC
I'm dowloading files over HTTP in multiple threads, after the files are downloaded I wait about 90-150 seconds and then I start downloading again.
After I start downloading again I get few of this exceptions:

An error occurred performing a WebClient request. 
InnerException : Object reference not set to an instance of an object

StackTrace :   at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponseAsyncCB2 (System.Net.WebAsyncResult aread) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.DownloadFileCore (System.Uri address, System.String fileName, System.Object userToken) [0x00000] in <filename unknown>:0 
  at System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName) [0x00000] in <filename unknown>:0 

I'm using mono 3.4.0 and I see that there was a rewrite of connection reuse and idle logic in this commit:
https://github.com/mono/mono/commit/c6a70a9586971d9d2561933fab47e30cca997e76
So I'm assuming that the bug was introduced in this commit.

When I increase ServicePointManager.MaxServicePointIdleTime I don't get this exception.
Comment 1 Christer Nordvik 2014-05-14 14:53:40 UTC
I get this a lot when using the latest Xamarin.iOS (7.2.2.2). Is this bug affecting Xamarin.iOS as well? Any workaround besides inceasing the IdleTime?
Comment 2 Christer Nordvik 2014-05-19 04:20:15 UTC
I added the IdleTime but still get this a lot and it has made us pull our update for our app. Really hope someone can prioritize this bug as it seems quite critical to me. 

Here is a stacktrace:

Object reference not set to an instance of an object - at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00000] in <filename unknown>:0 at System.Net.HttpWebRequest.GetResponseAsyncCB2 (System.Net.WebAsyncResult aread) [0x00000] in <filename unknown>:0 at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 at MyApp.io.AsyncHttp.Execute (MyApp.io.UrlParams urlParams) [0x00000] in <filename unknown>:0
Comment 3 Christer Nordvik 2014-05-20 03:42:21 UTC
I added an iOS specific bug here with a testcase: 
https://bugzilla.xamarin.com/show_bug.cgi?id=19914
Comment 4 Justin Toth 2014-06-12 13:02:47 UTC
I've been running into this error as well on Android, any progress?

System.NullReferenceException: Object reference not set to an instance of an object
[com.rpr.mobile]   at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Net.HttpWebRequest.GetResponseAsyncCB2 (System.Net.WebAsyncResult aread) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsyncBeginEnd (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsync (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsync (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state) [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Net.WebRequest.GetResponseAsync () [0x00000] in <filename unknown>:0 
[com.rpr.mobile]   at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x00000] in <filename unknown>:0
Comment 5 Miguel de Icaza [MSFT] 2014-06-12 14:11:17 UTC
Yes, this bug has been fixed, and it is going through our QA process as part of an update release.   Should be out soon.
Comment 6 Narinder 2014-06-23 12:19:10 UTC
Hi, 
I believe I run into the same exception on Debian :

at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.BeginGetRequestStream (System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetRequestStream () [0x00000] in <filename unknown>:0 
  at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke (System.String method_name, System.Object[] parameters) [0x00000] in <filename unknown>:0 
 
Can you say if possibly it's the same issue or should I investigate further ?

I am using Mono 3.4.0

Rgds
N
Comment 7 Narinder 2014-06-23 12:25:49 UTC
PS .. sorry I forgot to add that the exception is :

Object reference not set to an instance of an object
Comment 8 Narinder 2014-06-23 20:07:14 UTC
Created attachment 7158 [details]
Christer Nordvik's test case modified for Linux

Hi,
I attach a modified version of Christer Nordvik's test case ( https://bugzilla.xamarin.com/show_bug.cgi?id=19914) to reproduce problem on Mono 3.4.0  on Linux (Debian)

Mono JIT compiler version 3.4.0 (tarball Tue Apr  1 13:38:31 UTC 2014)
Copyright (C) 2002-2014 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:            sgen

The output of the modified test is as follows :


Hello World!
00:04:43 - 200
00:04:43 - 200
00:04:43 - 200
00:04:43 - 200
00:04:43 - 200
00:04:43 - 200
00:04:44 - 200
00:04:44 - 200
00:04:44 - 200
00:04:44 - 200
Hello World!
00:05:42 - 200
00:05:42 - 200
00:05:42 - 200
00:05:42 - 200
00:05:42 - 200
00:05:42 - 200
00:05:43 - 200
00:05:43 - 200
00:05:43 - 200
00:05:43 - 200
Hello World!
00:07:42 - 200
00:07:42 - 200
00:07:42 - 200
00:07:42 - 200
00:07:42 - 200
00:07:42 - 200
00:07:43 - 200
00:07:43 - 200
00:07:43 - 200
00:07:43 - 200
Hello World!

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00072] in /usr/src/packages/BUILD/mcs/class/System/System.Net/ServicePoint.cs:430 
  at System.Net.HttpWebRequest.GetResponseAsyncCB2 (System.Net.WebAsyncResult aread) [0x00094] in /usr/src/packages/BUILD/mcs/class/System/System.Net/HttpWebRequest.cs:964 
  at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x000e8] in /usr/src/packages/BUILD/mcs/class/System/System.Net/HttpWebRequest.cs:917 
  at CrashTest.io.AsyncHttp.Execute (CrashTest.io.UrlParams urlParams) [0x00084] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/CrashTest/AsyncHttp.cs:50 
  at CrashTest.CrashTestViewController.StartTest () [0x00024] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/CrashTest/CrashTestViewController.cs:28 
  at CCCC.MainClass.Main (System.String[] args) [0x00016] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/Program.cs:16 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at System.Net.ServicePoint.SendRequest (System.Net.HttpWebRequest request, System.String groupName) [0x00072] in /usr/src/packages/BUILD/mcs/class/System/System.Net/ServicePoint.cs:430 
  at System.Net.HttpWebRequest.GetResponseAsyncCB2 (System.Net.WebAsyncResult aread) [0x00094] in /usr/src/packages/BUILD/mcs/class/System/System.Net/HttpWebRequest.cs:964 
  at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x000e8] in /usr/src/packages/BUILD/mcs/class/System/System.Net/HttpWebRequest.cs:917 
  at CrashTest.io.AsyncHttp.Execute (CrashTest.io.UrlParams urlParams) [0x00084] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/CrashTest/AsyncHttp.cs:50 
  at CrashTest.CrashTestViewController.StartTest () [0x00024] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/CrashTest/CrashTestViewController.cs:28 
  at CCCC.MainClass.Main (System.String[] args) [0x00016] in /home/nclaire/temp/bugtest/CrashTest/CCCC/CCCC/Program.cs:16 

I'll run the same test under mono 3.6.0.

Rgds
N
Comment 9 PJ 2014-07-22 14:00:19 UTC
This was fixed in the HTTP hotfix release on June 20th, the Mono 3.4.0.214 release. 

The test case in comment 1 (plus the modification in comment 3) passes for me on our current stable release, Mono 3.4.0.214 [1]. It fails on the release before the HTTP hotfix release, Mono 3.4.0.204 [2].

Here is the commit with the fix: https://github.com/mono/mono/commit/959fd8c59f73be3bfd794342870e3034280a978e

That commit has also been applied to master: https://github.com/mono/mono/commit/bad5ef8453b25f9bf7e31487b37153e5a35669e8

[1] https://gist.github.com/pjbeaman/32e675c9c40006eabcfd
[2] https://gist.github.com/pjbeaman/bc5556c274dab84e294f