Score:0

Storageclient.ListObjects causes thread cancel error when deployed to server

lk flag

I have some code that fetches files from Google Cloud Storage. The code works fine when run on my local development machine but when deployed to our production server it always "stops" whenever the parameter "objectsInBucket" (in the code below) is used.

In the example below the last code that actually executes correctly when deployed to the server is the line "LogHelper.LogToConsole("-6").

If I "uncomment" the foreach loop the last output to console is "- 4". Also, if I for example make a variable like "var count = objectsInBucket.Count();" and put it immediately after "objectsInBucket = storageClient.Listobjects(_gcsBucketName);" then the last output will be "- 1". So basically, if I operate any method on the "objectsInBucket" it causes a Thread Cancel. Surrounding that piece of code with a try/catch does not make the code go into the catch either...

I've also tried fiddler to see if there are any outbound calls made when performing a method on the object but I have not managed to see any outbound calls being made.

But like I said, this problem only occurs when deployed to the server. So what can the cause of this be? Sorry for all the extensive calls to "LogHelper" in the provided code but they are there to show how far the code executes..

private void DownloadResourceFromGCS(string attachmentId)
    {
        LogHelper.LogToConsole($" - DownloadResourceFromGCS({attachmentId})");

        using (StorageClient storageClient = StorageClient.Create())
        {
            Google.Api.Gax.PagedEnumerable<Google.Apis.Storage.v1.Data.Objects, Google.Apis.Storage.v1.Data.Object> objectsInBucket = null;
           
            LogHelper.LogToConsole($" - 1");
            objectsInBucket = storageClient.ListObjects(_gcsBucketName);
            LogHelper.LogToConsole($" - 2 {JsonConvert.SerializeObject( objectsInBucket)}");
            var dirPath = Path.Combine(_gcsAttachmentPath, attachmentId);
            LogHelper.LogToConsole($" - 3");

            if (objectsInBucket != null)
            {
                LogHelper.LogToConsole($" - 4");

                var directoryInfo = new DirectoryInfo(dirPath);
                if (directoryInfo.Exists)
                {
                    LogHelper.LogToConsole($" - Deleting directory: {dirPath}");
                    directoryInfo.Delete(true);
                }
                directoryInfo.Create();
                LogHelper.LogToConsole($" - Directory created: {dirPath}");
            }
            LogHelper.LogToConsole($" - 6");
            IEnumerable<Google.Apis.Storage.v1.Data.Object> attachmentFiles = null;
                            
            LogHelper.LogToConsole($" - 7");
            attachmentFiles = objectsInBucket.ToList().Where(x => x.Name.Contains(attachmentId)).ToList();
            LogHelper.LogToConsole($" - 8");
            LogHelper.LogToConsole($" - {attachmentFiles.Count()} attachments found where name matches attachmentid {attachmentId}");
            LogHelper.LogToConsole($" - 9");

            foreach (var attachmentFile in attachmentFiles)
            //foreach (var attachmentFile in objectsInBucket)
            {
                LogHelper.LogToConsole($" - Processing attachment files");
                var extension = Path.GetExtension(attachmentFile.Name);
                // files/attachments may exists in the folder, but the folder name is also download as an object, but we don't need
                if (!string.IsNullOrWhiteSpace(extension) && attachmentFile.Name.Contains(attachmentId))
                {

                    var fileNames = attachmentFile.Name.Split('/');
                    var fileName = fileNames[1];
                    var filePath = Path.Combine(dirPath, fileName);

                    using (var outputFile = File.OpenWrite(filePath))
                    {
                        LogHelper.LogToConsole($" - Downloading attachment with name {fileName}");

                        try
                        {
                            storageClient.DownloadObject(_gcsBucketName, attachmentFile.Name, outputFile);
                            LogHelper.LogToConsole($" - Download of attachment with name {fileName} completed");
                        }
                        catch (Exception e)
                        {
                            LogHelper.LogToConsole($" - Failed to download attachment with name {fileName}");
                            throw;
                        }
                    }
                }
            }
        }

        LogHelper.LogToConsole($" - DownloadResourceFromGCS({attachmentId}) COMPLETED");
    }
Tobias Åkerlund avatar
lk flag
Update: I've previously deployed the application to two different production servers which both displayed the same behaviour/error. I recently deployed to a test-server which is not at all as restricted when it comes to firewalls etc and on this server the application works correctly. Therefore I suspect that the method makes some call that is blocked by the firewall on the production servers. I have the network team to check the logs for outgoing calls and hopefully something can be unblocked in the firewall.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.