Storage emulator crashes
[REQUIRED] Environment info
firebase-tools: 9.22.0
Platform: Mac
[REQUIRED] Test case
Developing a Swift app. Here is the main view where the storage emulator is connected:
import SwiftUI
import Firebase
@main
struct TestApp: App {
init() {
FirebaseApp.configure()
let settings = Firestore.firestore().settings
settings.host = "localhost:8080"
settings.isPersistenceEnabled = false
settings.isSSLEnabled = false
Firestore.firestore().settings = settings
Storage.storage().useEmulator(withHost:"localhost", port:9199)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
The bug occurs in trying to upload an image to storage in a test case. Here is the function that is called:
private static func upload_image_to_firestore(image: UIImage, fname_firebase_png: String, completion: @escaping (Result<Void,Error>) -> Void) {
// Create a root reference
let storageRef = storage.reference()
// Create a reference to 'images/user_id.jpg' - each user gets on profile image
let imageRef = storageRef.child(fname_firebase_png)
// Now upload the image
// Data in memory
let dataW : Data? = image.jpegData(compressionQuality: 0.5)
guard let data = dataW else {
logger.error("Error! Could not convert profile image to JPEG for upload")
completion(.failure(UnknownError.message("Error! Could not convert profile image to JPEG for upload")))
return
}
// Upload the file
imageRef.putData(data, metadata: nil) { (_, error) in
if let error = error {
logger.error("Error - could not upload image: \(fname_firebase_png) error: \(error.localizedDescription)")
completion(.failure(error))
} else {
logger.debug("Uploaded profile image to path: \(fname_firebase_png)")
completion(.success(()))
}
}
}
[REQUIRED] Steps to reproduce
Call the upload_image_to_firestore function from a unit test.
[REQUIRED] Expected behavior
The image should upload to the emulator storage.
[REQUIRED] Actual behavior
The emulator crashes with the message in the console:
Error: An unexpected error has occurred.
Error: An unexpected error has occurred.
Error: An unexpected error has occurred.
In the firebase-debug.log:
[debug] [2021-11-05T23:29:47.595Z] <<< [apiv2][status] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast 200
[debug] [2021-11-05T23:29:47.595Z] <<< [apiv2][body] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast {"status":"multicast_acknowledged"}
[debug] [2021-11-05T23:29:47.595Z] >>> [apiv2][query] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast [none]
[debug] [2021-11-05T23:29:47.595Z] >>> [apiv2][body] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast "{\"specVersion\":1,\"type\":\"google.cloud.storage.object.v1.deleted\",\"source\":\"//storage.googleapis.com/projects/_/buckets/APPNAME.appspot.com/objects/machines/oUAHvKg3rTjQMmJoP43a/image.png\",\"data\":{\"kind\":\"#storage#object\",\"name\":\"machines/oUAHvKg3rTjQMmJoP43a/image.png\",\"bucket\":\"APPNAME.appspot.com\",\"generation\":\"1636154987585\",\"metageneration\":\"1\",\"contentType\":\"application/octet-stream\",\"timeCreated\":\"2021-11-05T16:29:47.584Z\",\"updated\":\"2021-11-05T16:29:47.584Z\",\"storageClass\":\"STANDARD\",\"size\":\"5464\",\"md5Hash\":\"r9tU1HQyFqL86krMqT5/VQ==\",\"etag\":\"someETag\",\"crc32c\":\"----wQ==\",\"timeStorageClassUpdated\":\"2021-11-05T16:29:47.584Z\",\"id\":\"APPNAME.appspot.com/machines/oUAHvKg3rTjQMmJoP43a/image.png/1636154987585\",\"selfLink\":\"http://localhost:9199/storage/v1/b/APPNAME.appspot.com/o/machines%2FoUAHvKg3rTjQMmJoP43a%2Fimage.png\",\"mediaLink\":\"http://localhost:9199/download/storage/v1/b/APPNAME.appspot.com/o/machines%2FoUAHvKg3rTjQMmJoP43a%2Fimage.png?generation=1636154987585&alt=media\"}}"
[debug] [2021-11-05T23:29:47.598Z] <<< [apiv2][status] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast 200
[debug] [2021-11-05T23:29:47.598Z] <<< [apiv2][body] POST http://localhost:5001/functions/projects/APPNAME/trigger_multicast {"status":"multicast_acknowledged"}
[debug] [2021-11-05T23:29:47.601Z] Error: ENOENT: no such file or directory, unlink '/var/folders/d4/tc0mxg2s2ts4rk0vmcsc7lbw0000gn/T/firebase/storage/blobs/00d7606c-fcfc-421d-aa23-0523b99fa410_b_APPNAME.appspot.com_o_machines%2FoUAHvKg3rTjQMmJoP43a%2Fimage.png'
at Object.unlinkSync (node:fs:1718:3)
at Persistence.deleteFile (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/files.js:478:18)
at StorageLayer.cancelUpload (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/files.js:152:27)
at handleUpload (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/apis/firebase.js:366:45)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/apis/firebase.js:443:24
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/layer.js:95:5)
at next (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/layer.js:95:5)
at /usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:281:22
at param (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:354:14)
at param (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:365:14)
at param (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:410:3)
at next (/usr/local/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:275:10)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/apis/firebase.js:89:9
[error]
[error] Error: An unexpected error has occurred.
where I have scrubbed the APPNAME.
After this, the port is also left occupied. Trying to run firebase emulators:start again gives:
⚠ firestore: Port 8080 is not open on localhost, could not start Firestore Emulator.
and the port has to manually closed by finding the process ID using lsof -i :8080 and killing it.
Related #3550
I believe this issue is a duplicate of https://github.com/firebase/firebase-tools/issues/3550, which should be fixed by https://github.com/firebase/firebase-tools/pull/4210.