Skip to content

Commit e7b05e2

Browse files
committed
Fix WebSocket memory free to be thread safe.
For U504-028.
1 parent 9e5bad5 commit e7b05e2

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/core/aws-net-websocket-registry.adb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Ada Web Server --
33
-- --
4-
-- Copyright (C) 2012-2019, AdaCore --
4+
-- Copyright (C) 2012-2021, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify --
77
-- it under terms of the GNU General Public License as published by the --
@@ -161,10 +161,14 @@ package body AWS.Net.WebSocket.Registry is
161161
entry Get_Socket (WebSocket : out Object_Class);
162162
-- Get a WebSocket having some data to be sent
163163

164-
procedure Release_Socket (WebSocket : Object_Class);
164+
procedure Release_Socket (WebSocket : in out Object_Class);
165165
-- Release a socket retrieved with Get_Socket above, this socket will be
166166
-- then available again.
167167

168+
procedure Free (WebSocket : in out Object_Class);
169+
-- Free WebSocket immediately if not taken by another task, otherwise
170+
-- record it to be freed as soon as it is released.
171+
168172
entry Not_Empty;
169173
-- Returns if the Set is not empty
170174

@@ -329,7 +333,7 @@ package body AWS.Net.WebSocket.Registry is
329333

330334
procedure Do_Free (WebSocket : in out Object_Class) is
331335
begin
332-
Unchecked_Free (WebSocket);
336+
DB.Free (WebSocket);
333337
end Do_Free;
334338

335339
-----------------
@@ -568,6 +572,24 @@ package body AWS.Net.WebSocket.Registry is
568572
Registered.Clear;
569573
end Finalize;
570574

575+
----------
576+
-- Free --
577+
----------
578+
579+
procedure Free (WebSocket : in out Object_Class) is
580+
begin
581+
-- If WebSocket is in Sending it means that it has been
582+
-- taken by the Get_Socket call. We cannot free it now, we
583+
-- record this socket to be freed as soon as it is released
584+
-- (Release_Socket) call.
585+
586+
if Sending.Contains (WebSocket.Id) then
587+
WebSocket.To_Free := True;
588+
else
589+
Unchecked_Free (WebSocket);
590+
end if;
591+
end Free;
592+
571593
----------------
572594
-- Get_Socket --
573595
----------------
@@ -738,10 +760,19 @@ package body AWS.Net.WebSocket.Registry is
738760
-- Release_Socket --
739761
--------------------
740762

741-
procedure Release_Socket (WebSocket : Object_Class) is
763+
procedure Release_Socket (WebSocket : in out Object_Class) is
742764
begin
743765
Sending.Exclude (WebSocket.Id);
744-
New_Pending := True;
766+
767+
-- The socket has been recorded to be freed. It is not anymore
768+
-- in the registry, we just need to free it now that it has
769+
-- been released.
770+
771+
if WebSocket.To_Free then
772+
Unchecked_Free (WebSocket);
773+
else
774+
New_Pending := True;
775+
end if;
745776
end Release_Socket;
746777

747778
------------

src/core/aws-net-websocket.ads

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Ada Web Server --
33
-- --
4-
-- Copyright (C) 2012-2020, AdaCore --
4+
-- Copyright (C) 2012-2021, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify --
77
-- it under terms of the GNU General Public License as published by the --
@@ -289,6 +289,7 @@ private
289289
Messages : Message_List.List;
290290
Mem_Sock : Net.Socket_Access;
291291
In_Mem : Boolean := False;
292+
To_Free : Boolean := False;
292293

293294
Connection : AWS.Client.HTTP_Connection_Access;
294295
-- Only set when the web socket is initialized as a client.
@@ -366,7 +367,8 @@ private
366367
Messages => Message_List.Empty_List,
367368
Mem_Sock => null,
368369
Connection => null,
369-
In_Mem => False);
370+
In_Mem => False,
371+
To_Free => False);
370372

371373
-- Error codes corresponding to all errors
372374

0 commit comments

Comments
 (0)