Bug 28157 - UIApplication.SharedApplication.BeginBackgroundTask ( () => { }); does not call EndBackgroundTask per Xamarin Documentation
Summary: UIApplication.SharedApplication.BeginBackgroundTask ( () => { }); does not ca...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.8.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: CraigD
Depends on:
Reported: 2015-03-18 12:28 UTC by Paul Auman
Modified: 2017-05-25 22:06 UTC (History)
5 users (show)

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

Test Case #1 Fail, Test Case #2 Pass - Run on Device (10.34 KB, application/zip)
2015-03-18 12:28 UTC, Paul Auman

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 Developer Community or GitHub 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:

Description Paul Auman 2015-03-18 12:28:57 UTC
Created attachment 10399 [details]
Test Case #1 Fail, Test Case #2 Pass - Run on Device

See attached sample. 

// test case #1 - this example per xamarmin documentation will through an exception once the expirationHandler fires

taskId = UIApplication.SharedApplication.BeginBackgroundTask ( () => { });

Mar 18 12:15:15 Pauls-iPhone assertiond[63] <Warning>: <BKNewProcess: 0x13667ce50; com.your-company.TestBackgroundThreads; pid: 2563; hostpid: -1> has active assertions beyond permitted time:
<BKProcessAssertion: 0x1365178f0> id: 2563-32160783-BF54-4132-BD7D-32BA50B22B1B name: Called by TestBackgroundThreads, from wrapper_managed_to_native_MonoTouch_ObjCRuntime_Messaging_int_objc_msgSend_IntPtr_intptr_intptr_intptr process: <BKNewProcess: 0x13667ce50; com.your-company.TestBackgroundThreads; pid: 2563; hostpid: -1> permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:2563 preventSuspend  preventIdleSleep  preventSuspendOnSleep

// ref : http://iosapi.xamarin.com/index.aspx?link=M%3AMonoTouch.UIKit.UIApplication.BeginBackgroundTask(MonoTouch.Foundation.NSAction)
// error in documentation ->  it implies that EndBackgroundTask is called in the handler.
// ref : http://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/ios_backgrounding_with_tasks/
// also  -monotouch samples does not test for expired condition

// test case #2 - working per apple documentation

taskId = UIApplication.SharedApplication.BeginBackgroundTask ( () => { 

				// will occasioiniall generate an exception
				Console.WriteLine("Expiration Handler for taskId {0} fired", taskId);

				// must mark the long running background task ended, or a fault fires
				UIApplication.SharedApplication.EndBackgroundTask (taskId);

				// everything after the ABOVE method is may be called on this run loop on suspened until the app resumes.
				Console.WriteLine("Task Timedout {0} {1}", taskId, t.Status.ToString());

		// ref - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:
			- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler

			A handler to be called shortly before the app’s remaining background time reaches 0. You should use this handler to clean up 
			and mark the end of the background task. Failure to end the task explicitly will result in the termination of the app. 
			The handler is called synchronously on the main thread, blocking the app’s suspension momentarily while the app is notified.



Paul Auman
Auman Software, LLC

=== Xamarin Studio ===

Version 5.8 (build 443)
Installation UUID: acc29f18-0e02-4fed-8682-80df4d204c50
	Mono 3.12.1 ((detached/b7764aa)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010000

=== Xamarin.Android ===

Not Installed

=== Xamarin Android Player ===

Not Installed

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: ccfcd59
Build date: 2015-03-10 02:20:32-0400

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 508000443
Git revision: 73883239470cbe8e261c94d95f7c3d0452fd393b
Build date: 2015-03-10 07:22:51-04
Xamarin addins: a2ff7b617f09d9c45d8bbf3d010b5db0d7d36100

=== Operating System ===

Mac OS X 10.10.0
Darwin PRA2D-3.local 14.0.0 Darwin Kernel Version 14.0.0
    Mon Aug 25 20:37:34 PDT 2014
    root:xnu-2782. x86_64
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-03-18 12:46:41 UTC
(In reply to comment #0)
> // ref :
> http://iosapi.xamarin.com/index.aspx?link=M%3AMonoTouch.UIKit.UIApplication.BeginBackgroundTask(MonoTouch.Foundation.NSAction)
> // error in documentation ->  it implies that EndBackgroundTask is called in the handler.

I've read that page, and it seems clear to me that Xamarin.iOS does not call EndBackgroundTask in the handler.

Exactly which formulation makes you think that?
Comment 2 Paul Auman 2015-03-18 14:34:44 UTC

Specifically the quoted text below. 

But more to my point, none of your samples fully suspend the app cleanly. If by design then change the sample to show that taskId needs closure.


From :


>> Avoiding App Termination With Expiration Handlers

>>In addition to giving access to the BackgroundTimeRemaining property, iOS provides a graceful way to handle background time expiration through an Expiration Handler. This is an optional block of code that will get executed when the time allotted for a task is about to expire.
>> Code in the Expiration Handler calls EndBackgroundTask and passes in the task ID, which indicates that the app is behaving well and prevents iOS from terminating the app even if the task runs out of time.
>>The expiration handler is expressed as an anonymous function using a lambda expression, as illustrated by the BeginBackgroundTask call below:
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-03-18 14:43:06 UTC
@Craig, this looks like a documentation issue, can you take over?
Comment 4 CraigD 2015-03-19 16:12:32 UTC
Assigned for investigation & update.
Comment 5 Larry O'Brien 2015-03-19 19:15:48 UTC
Behavior confirmed (Paul's test cases are very clear).

API docs updated in b8c998e0b214d51224f555989494f46e8f323f94