Skip to content

StorageRef.putFile() doesn't work but putData() does, when running in a unit test #14695

Open
@natep

Description

@natep

Description

I'll preface this by saying that I am using the Firebase emulator. I am also running this code in a unit test that is not running inside the context of an app.

I'm running into a problem when attempting to upload files to Storage using the iOS API. When I run this code:

// `zipFile` is a URL for a file that exists locally in a temp directory
let fileRef = cloudStorageRoot.child("test.zip")
let _ = try await fileRef.putFileAsync(from: zipFile)

I get the following error:

Task <6EBDD3A8-E1A0-4584-ABAB-0E5E5DBA78F3>.<1> finished with error [-1] Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLStringKey=http://127.0.0.1:9199/v0/b/abc-xyz-123.storage.firebase.com/o?name=test.zip&upload_id=b5345624-594c-4007-8267-e42bcbeb0dfa&upload_protocol=resumable, NSErrorFailingURLKey=http://127.0.0.1:9199/v0/b/abc-xyz-123.storage.firebase.com/o?name=test.zip&upload_id=b5345624-594c-4007-8267-e42bcbeb0dfa&upload_protocol=resumable, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "BackgroundUploadTask <6EBDD3A8-E1A0-4584-ABAB-0E5E5DBA78F3>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <6EBDD3A8-E1A0-4584-ABAB-0E5E5DBA78F3>.<1>, NSLocalizedDescription=unknown error}

I also see numerous copies of the following error:

BackgroundSession <633A02F2-A4F3-4509-B711-D103AD425ABA> an error occurred on the xpc connection to setup the background session: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.nsurlsessiond" UserInfo={NSDebugDescription=connection to service named com.apple.nsurlsessiond}
BackgroundSession <633A02F2-A4F3-4509-B711-D103AD425ABA> failed to create a background NSURLSessionUploadTask, as remote session is unavailable

However, if I change putFile to putData (and leave everything else exactly the same), the upload succeeds:

let fileRef = cloudStorageRoot.child("test.zip")
let zipData = try Data(contentsOf: zipFile)
let _ = try await fileRef.putDataAsync(zipData)

Based on this post from the Apple dev forums, it seems that the problem might be that the SDK is using URLSession background sessions, which do not work outside the context of an app.

Would it be possible for the SDK to detect if it fails to create a background URLSession, and fallback to a normal session in that case? Or is there some way I can force it to use a regular session while running in unit tests?

Reproducing the issue

No response

Firebase SDK Version

11.7

Xcode Version

16.2

Installation Method

Swift Package Manager

Firebase Product(s)

Storage

Targeted Platforms

iOS

Relevant Log Output

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
{
  "originHash" : "8088edbf6bbac4606f7ec39fe23eed55b8f3b94c21bd94e9a78194551b46f8c8",
  "pins" : [
    {
      "identity" : "abseil-cpp-binary",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/abseil-cpp-binary.git",
      "state" : {
        "revision" : "194a6706acbd25e4ef639bcaddea16e8758a3e27",
        "version" : "1.2024011602.0"
      }
    },
    {
      "identity" : "app-check",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/app-check.git",
      "state" : {
        "revision" : "61b85103a1aeed8218f17c794687781505fbbef5",
        "version" : "11.2.0"
      }
    },
    {
      "identity" : "cocoalumberjack",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/CocoaLumberjack/CocoaLumberjack.git",
      "state" : {
        "revision" : "4b8714a7fb84d42393314ce897127b3939885ec3",
        "version" : "3.8.5"
      }
    },
    {
      "identity" : "firebase-ios-sdk",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/firebase-ios-sdk.git",
      "state" : {
        "revision" : "0d885d28250fb1196b614bc9455079b75c531f72",
        "version" : "11.7.0"
      }
    },
    {
      "identity" : "googleappmeasurement",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleAppMeasurement.git",
      "state" : {
        "revision" : "be0881ff728eca210ccb628092af400c086abda3",
        "version" : "11.7.0"
      }
    },
    {
      "identity" : "googledatatransport",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleDataTransport.git",
      "state" : {
        "revision" : "617af071af9aa1d6a091d59a202910ac482128f9",
        "version" : "10.1.0"
      }
    },
    {
      "identity" : "googleutilities",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleUtilities.git",
      "state" : {
        "revision" : "53156c7ec267db846e6b64c9f4c4e31ba4cf75eb",
        "version" : "8.0.2"
      }
    },
    {
      "identity" : "grpc-binary",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/grpc-binary.git",
      "state" : {
        "revision" : "f56d8fc3162de9a498377c7b6cea43431f4f5083",
        "version" : "1.65.1"
      }
    },
    {
      "identity" : "gtm-session-fetcher",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/gtm-session-fetcher.git",
      "state" : {
        "revision" : "85b7b231882c3c472b8bda4fb495324d3f19bab6",
        "version" : "4.2.0"
      }
    },
    {
      "identity" : "interop-ios-for-google-sdks",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/interop-ios-for-google-sdks.git",
      "state" : {
        "revision" : "2d12673670417654f08f5f90fdd62926dc3a2648",
        "version" : "100.0.0"
      }
    },
    {
      "identity" : "leveldb",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/leveldb.git",
      "state" : {
        "revision" : "a0bc79961d7be727d258d33d5a6b2f1023270ba1",
        "version" : "1.22.5"
      }
    },
    {
      "identity" : "nanopb",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/nanopb.git",
      "state" : {
        "revision" : "b7e1104502eca3a213b46303391ca4d3bc8ddec1",
        "version" : "2.30910.0"
      }
    },
    {
      "identity" : "promises",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/promises.git",
      "state" : {
        "revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac",
        "version" : "2.4.0"
      }
    },
    {
      "identity" : "swift-log",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-log",
      "state" : {
        "revision" : "96a2f8a0fa41e9e09af4585e2724c4e825410b91",
        "version" : "1.6.2"
      }
    },
    {
      "identity" : "swift-protobuf",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-protobuf.git",
      "state" : {
        "revision" : "ebc7251dd5b37f627c93698e4374084d98409633",
        "version" : "1.28.2"
      }
    },
    {
      "identity" : "zipfoundation",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/weichsel/ZIPFoundation.git",
      "state" : {
        "revision" : "02b6abe5f6eef7e3cbd5f247c5cc24e246efcfe0",
        "version" : "0.9.19"
      }
    }
  ],
  "version" : 3
}

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions