Skip to content

Commit a8cc159

Browse files
committed
Dispose tmp file handles after sync handling
1 parent f830a0c commit a8cc159

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

Signal-Windows.Lib/IncomingMessages.cs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -186,71 +186,75 @@ private void HandleMessage(SignalServiceEnvelope envelope)
186186
}
187187
else if (content.SynchronizeMessage.Groups != null)
188188
{
189-
//return;
189+
Logger.LogInformation("HandleMessage() handling groups sync message from device {0}", envelope.GetSourceDevice());
190190
int read;
191191
var avatarBuffer = new byte[4096];
192192
var groups = content.SynchronizeMessage.Groups;
193-
var tmpFile = LibUtils.CreateTmpFile("groups_sync");
194-
var plaintextStream = MessageReceiver.RetrieveAttachment(groups.AsPointer(), tmpFile, 10000, null);
195-
var deviceGroupsStream = new DeviceGroupsInputStream(plaintextStream);
196-
var groupsList = new List<(SignalGroup, IList<string>)>();
197-
DeviceGroup g;
198-
while ((g = deviceGroupsStream.Read()) != null)
193+
using (var tmpFile = LibUtils.CreateTmpFile("groups_sync"))
199194
{
200-
if (g.Avatar != null)
195+
var plaintextStream = MessageReceiver.RetrieveAttachment(groups.AsPointer(), tmpFile, 10000, null);
196+
var deviceGroupsStream = new DeviceGroupsInputStream(plaintextStream);
197+
var groupsList = new List<(SignalGroup, IList<string>)>();
198+
DeviceGroup g;
199+
while ((g = deviceGroupsStream.Read()) != null)
201200
{
202-
SignalServiceAttachmentStream ssas = g.Avatar.AsStream();
203-
while ((read = ssas.InputStream.Read(avatarBuffer, 0, avatarBuffer.Length)) > 0)
201+
if (g.Avatar != null)
204202
{
203+
SignalServiceAttachmentStream ssas = g.Avatar.AsStream();
204+
while ((read = ssas.InputStream.Read(avatarBuffer, 0, avatarBuffer.Length)) > 0)
205+
{
205206

207+
}
206208
}
209+
var group = new SignalGroup()
210+
{
211+
ThreadDisplayName = g.Name,
212+
ThreadId = Base64.EncodeBytes(g.Id),
213+
GroupMemberships = new List<GroupMembership>(),
214+
CanReceive = true,
215+
ExpiresInSeconds = g.ExpirationTimer != null ? g.ExpirationTimer.Value : 0
216+
};
217+
groupsList.Add((group, g.Members));
207218
}
208-
var group = new SignalGroup()
209-
{
210-
ThreadDisplayName = g.Name,
211-
ThreadId = Base64.EncodeBytes(g.Id),
212-
GroupMemberships = new List<GroupMembership>(),
213-
CanReceive = true,
214-
ExpiresInSeconds = g.ExpirationTimer != null ? g.ExpirationTimer.Value : 0
215-
};
216-
groupsList.Add((group, g.Members));
219+
List<SignalConversation> newConversations = SignalDBContext.InsertOrUpdateGroups(groupsList);
220+
SignalLibHandle.Instance.DispatchAddOrUpdateConversations(newConversations);
217221
}
218-
List<SignalConversation> newConversations = SignalDBContext.InsertOrUpdateGroups(groupsList);
219-
SignalLibHandle.Instance.DispatchAddOrUpdateConversations(newConversations);
220222
}
221223
else if (content.SynchronizeMessage.Contacts != null && content.SynchronizeMessage.Contacts.Complete) //TODO incomplete updates
222224
{
223-
//return;
225+
Logger.LogInformation("HandleMessage() handling contacts sync message from device {0}", envelope.GetSourceDevice());
224226
int read;
225227
var avatarBuffer = new byte[4096];
226228
ContactsMessage contacts = content.SynchronizeMessage.Contacts;
227-
var tmpFile = LibUtils.CreateTmpFile("contacts_sync");
228-
var plaintextStream = MessageReceiver.RetrieveAttachment(contacts.Contacts.AsPointer(), tmpFile, 10000, null);
229-
var deviceContactsStream = new DeviceContactsInputStream(plaintextStream);
230-
List<SignalContact> contactsList = new List<SignalContact>();
231-
DeviceContact c;
232-
while ((c = deviceContactsStream.Read()) != null)
229+
using (var tmpFile = LibUtils.CreateTmpFile("contacts_sync"))
233230
{
234-
if (c.Avatar != null)
231+
var plaintextStream = MessageReceiver.RetrieveAttachment(contacts.Contacts.AsPointer(), tmpFile, 10000, null);
232+
var deviceContactsStream = new DeviceContactsInputStream(plaintextStream);
233+
List<SignalContact> contactsList = new List<SignalContact>();
234+
DeviceContact c;
235+
while ((c = deviceContactsStream.Read()) != null)
235236
{
236-
SignalServiceAttachmentStream ssas = c.Avatar.AsStream();
237-
while ((read = ssas.InputStream.Read(avatarBuffer, 0, avatarBuffer.Length)) > 0)
237+
if (c.Avatar != null)
238238
{
239+
SignalServiceAttachmentStream ssas = c.Avatar.AsStream();
240+
while ((read = ssas.InputStream.Read(avatarBuffer, 0, avatarBuffer.Length)) > 0)
241+
{
239242

243+
}
240244
}
245+
SignalContact contact = new SignalContact()
246+
{
247+
ThreadDisplayName = c.Name,
248+
ThreadId = c.Number,
249+
Color = c.Color,
250+
CanReceive = true,
251+
ExpiresInSeconds = c.ExpirationTimer != null ? c.ExpirationTimer.Value : 0
252+
};
253+
contactsList.Add(contact);
241254
}
242-
SignalContact contact = new SignalContact()
243-
{
244-
ThreadDisplayName = c.Name,
245-
ThreadId = c.Number,
246-
Color = c.Color,
247-
CanReceive = true,
248-
ExpiresInSeconds = c.ExpirationTimer != null ? c.ExpirationTimer.Value : 0
249-
};
250-
contactsList.Add(contact);
255+
var newConversations = SignalDBContext.InsertOrUpdateContacts(contactsList);
256+
SignalLibHandle.Instance.DispatchAddOrUpdateConversations(newConversations);
251257
}
252-
var newConversations = SignalDBContext.InsertOrUpdateContacts(contactsList);
253-
SignalLibHandle.Instance.DispatchAddOrUpdateConversations(newConversations);
254258
}
255259
}
256260
else if (content.ReadMessage != null)

0 commit comments

Comments
 (0)