Commit fa7f7e2
Fix port ID reuse when quickly removing and adding conference bridge ports (#4722)
* Initial plan
* Fix port ID reuse by freeing slot after removal callback
Move conf slot freeing from op_remove_port() to new op_remove_port2()
function that is called after the callback in handle_op_queue(). This
ensures port IDs remain unique - a slot won't be reused until after
the removal callback has been invoked.
Changes:
- Added op_remove_port2() to free the conf slot after callback
- Modified handle_op_queue() to call op_remove_port2() after callback for REMOVE_PORT ops
- Removed slot freeing code from op_remove_port() (moved to op_remove_port2())
- Updated log messages to distinguish "Removing" vs "Removed" states
Co-authored-by: nanangizz <[email protected]>
* Apply slot freeing after callback to synchronous removals too
Also call op_remove_port2() after callback in synchronous port removal
cases (pjmedia_conf_destroy and immediate removal in pjmedia_conf_remove_port).
This ensures consistent behavior whether removal is queued or synchronous.
Co-authored-by: nanangizz <[email protected]>
* Address code review feedback: fix mutex and add warning log
- Read port_cnt before releasing mutex to avoid inconsistent log output
- Add warning log when port is already freed to diagnose unexpected scenarios
Co-authored-by: nanangizz <[email protected]>
* Apply port ID reuse fix to conf_thread.c and vid_conf.c
Extended the fix for port ID reuse to conf_thread.c and vid_conf.c:
- Added op_remove_port2() function to both files
- Modified handle_op_queue() to call op_remove_port2() after callback
- Updated synchronous removal paths in destroy functions
- Moved slot freeing from op_remove_port() to op_remove_port2()
This ensures port IDs remain unique across all conference bridge implementations.
Co-authored-by: nanangizz <[email protected]>
* Fix heap-use-after-free in op_remove_port2
Moved port count decrement and active_listener cleanup from op_remove_port2()
to op_remove_port() to happen BEFORE the port is destroyed. This prevents
accessing freed memory in op_remove_port2().
The issue was that op_remove_port() would destroy the conf_port structure,
then op_remove_port2() would try to access conf_port->is_new from the freed
structure, causing a heap-use-after-free error detected by AddressSanitizer.
Fixed in all three conference bridge implementations:
- conference.c
- conf_thread.c
- vid_conf.c
Co-authored-by: nanangizz <[email protected]>
* Move port destruction to op_remove_port2 for consistent port count
Moved port destruction and port count decrement from op_remove_port() to
op_remove_port2() so that these operations happen after the callback is
invoked. This ensures the port count remains consistent throughout the
removal process.
Now the flow is:
1. op_remove_port() - Disconnects port and prepares for removal
2. Callback is invoked
3. op_remove_port2() - Frees slot, updates port count, and destroys port
This prevents the port count from changing before the callback, which could
confuse applications monitoring port counts.
Applied to all three conference bridge implementations.
Co-authored-by: nanangizz <[email protected]>
* Move active_listener cleanup to op_remove_port for clarity
Moved the active_listener array cleanup from op_remove_port2() to
op_remove_port() for better code clarity. This operation doesn't need
to happen after the callback and doesn't access any freed memory, so
it's safer and clearer to do it in op_remove_port().
Now op_remove_port2() is focused on only the very final steps:
- Free the slot
- Update port count
- Destroy the port
This makes the code easier to understand and maintain.
Co-authored-by: nanangizz <[email protected]>
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: nanangizz <[email protected]>1 parent 9ffdbd7 commit fa7f7e2
3 files changed
+146
-36
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
477 | 477 | | |
478 | 478 | | |
479 | 479 | | |
| 480 | + | |
| 481 | + | |
480 | 482 | | |
481 | 483 | | |
482 | 484 | | |
| |||
557 | 559 | | |
558 | 560 | | |
559 | 561 | | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
560 | 567 | | |
561 | 568 | | |
562 | 569 | | |
| |||
1298 | 1305 | | |
1299 | 1306 | | |
1300 | 1307 | | |
| 1308 | + | |
| 1309 | + | |
1301 | 1310 | | |
1302 | 1311 | | |
1303 | 1312 | | |
| |||
2333 | 2342 | | |
2334 | 2343 | | |
2335 | 2344 | | |
| 2345 | + | |
| 2346 | + | |
| 2347 | + | |
2336 | 2348 | | |
2337 | 2349 | | |
2338 | 2350 | | |
| |||
2404 | 2416 | | |
2405 | 2417 | | |
2406 | 2418 | | |
2407 | | - | |
2408 | | - | |
2409 | | - | |
2410 | | - | |
2411 | | - | |
2412 | | - | |
2413 | | - | |
2414 | 2419 | | |
| 2420 | + | |
2415 | 2421 | | |
2416 | 2422 | | |
2417 | 2423 | | |
| |||
2425 | 2431 | | |
2426 | 2432 | | |
2427 | 2433 | | |
2428 | | - | |
2429 | | - | |
2430 | | - | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
2431 | 2467 | | |
2432 | 2468 | | |
2433 | 2469 | | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
2434 | 2475 | | |
2435 | 2476 | | |
2436 | 2477 | | |
2437 | | - | |
| 2478 | + | |
2438 | 2479 | | |
2439 | 2480 | | |
2440 | 2481 | | |
2441 | 2482 | | |
2442 | | - | |
2443 | | - | |
2444 | 2483 | | |
2445 | 2484 | | |
2446 | 2485 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| 295 | + | |
| 296 | + | |
295 | 297 | | |
296 | 298 | | |
297 | 299 | | |
| |||
373 | 375 | | |
374 | 376 | | |
375 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
376 | 383 | | |
377 | 384 | | |
378 | 385 | | |
| |||
920 | 927 | | |
921 | 928 | | |
922 | 929 | | |
| 930 | + | |
| 931 | + | |
923 | 932 | | |
924 | 933 | | |
925 | 934 | | |
| |||
1834 | 1843 | | |
1835 | 1844 | | |
1836 | 1845 | | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
1837 | 1849 | | |
1838 | 1850 | | |
1839 | 1851 | | |
| |||
1926 | 1938 | | |
1927 | 1939 | | |
1928 | 1940 | | |
1929 | | - | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
1930 | 1958 | | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
1931 | 1969 | | |
1932 | | - | |
1933 | 1970 | | |
| 1971 | + | |
1934 | 1972 | | |
1935 | 1973 | | |
1936 | 1974 | | |
1937 | | - | |
1938 | | - | |
1939 | | - | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
1940 | 1979 | | |
1941 | | - | |
| 1980 | + | |
1942 | 1981 | | |
1943 | 1982 | | |
1944 | 1983 | | |
1945 | 1984 | | |
1946 | | - | |
1947 | | - | |
1948 | 1985 | | |
1949 | 1986 | | |
1950 | 1987 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
150 | 152 | | |
151 | 153 | | |
152 | 154 | | |
| |||
234 | 236 | | |
235 | 237 | | |
236 | 238 | | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
237 | 244 | | |
238 | 245 | | |
239 | 246 | | |
| |||
375 | 382 | | |
376 | 383 | | |
377 | 384 | | |
| 385 | + | |
| 386 | + | |
378 | 387 | | |
379 | 388 | | |
380 | 389 | | |
| |||
755 | 764 | | |
756 | 765 | | |
757 | 766 | | |
758 | | - | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
759 | 791 | | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
760 | 802 | | |
761 | | - | |
762 | 803 | | |
| 804 | + | |
763 | 805 | | |
764 | 806 | | |
765 | 807 | | |
766 | | - | |
767 | | - | |
768 | | - | |
| 808 | + | |
769 | 809 | | |
770 | | - | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
771 | 814 | | |
772 | 815 | | |
773 | 816 | | |
774 | 817 | | |
775 | | - | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
780 | | - | |
781 | | - | |
782 | | - | |
783 | | - | |
784 | 818 | | |
785 | 819 | | |
786 | 820 | | |
| |||
0 commit comments