Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible race condition #702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion decryptor/postgresql/pending_packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ package postgresql
import (
"container/list"
"errors"
"reflect"
"sync"

"github.com/jackc/pgx/v5/pgproto3"
log "github.com/sirupsen/logrus"
"reflect"
)

// pendingPacketsList stores objects per their type and provides API similar to queue
type pendingPacketsList struct {
mutex sync.RWMutex
lists map[reflect.Type]*list.List
}

Expand All @@ -41,6 +44,9 @@ var ErrRemoveFromEmptyPendingList = errors.New("removing from empty pending list

// Add packet to pending list of packets of this type
func (packets *pendingPacketsList) Add(packet interface{}) error {
packets.mutex.Lock()
defer packets.mutex.Unlock()

switch packet.(type) {
case *ParsePacket, *BindPacket, *ExecutePacket, *pgproto3.RowDescription, *pgproto3.ParameterDescription, queryPacket:
packetType := reflect.TypeOf(packet)
Expand All @@ -58,6 +64,9 @@ func (packets *pendingPacketsList) Add(packet interface{}) error {

// RemoveNextPendingPacket removes first in the list pending packet
func (packets *pendingPacketsList) RemoveNextPendingPacket(packet interface{}) error {
packets.mutex.Lock()
defer packets.mutex.Unlock()

switch packet.(type) {
case *ParsePacket, *BindPacket, *ExecutePacket, *pgproto3.RowDescription, *pgproto3.ParameterDescription, queryPacket:
packetType := reflect.TypeOf(packet)
Expand All @@ -78,6 +87,9 @@ func (packets *pendingPacketsList) RemoveNextPendingPacket(packet interface{}) e

// RemoveAll pending packets of packet's type
func (packets *pendingPacketsList) RemoveAll(packet interface{}) error {
packets.mutex.Lock()
defer packets.mutex.Unlock()

switch packet.(type) {
case *ParsePacket, *BindPacket, *ExecutePacket, *pgproto3.RowDescription, *pgproto3.ParameterDescription, queryPacket:
packetList, ok := packets.lists[reflect.TypeOf(packet)]
Expand All @@ -93,6 +105,9 @@ func (packets *pendingPacketsList) RemoveAll(packet interface{}) error {

// GetPendingPacket returns next pending packet
func (packets *pendingPacketsList) GetPendingPacket(packet interface{}) (interface{}, error) {
packets.mutex.RLock()
defer packets.mutex.RUnlock()

switch packet.(type) {
case *ParsePacket, *BindPacket, *ExecutePacket, *pgproto3.RowDescription, *pgproto3.ParameterDescription, queryPacket:
packetType := reflect.TypeOf(packet)
Expand All @@ -112,6 +127,9 @@ func (packets *pendingPacketsList) GetPendingPacket(packet interface{}) (interfa

// GetLastPending return last added pending packet
func (packets *pendingPacketsList) GetLastPending(packet interface{}) (interface{}, error) {
packets.mutex.RLock()
defer packets.mutex.RUnlock()

switch packet.(type) {
case *ParsePacket, *BindPacket, *ExecutePacket, *pgproto3.RowDescription, *pgproto3.ParameterDescription, queryPacket:
packetType := reflect.TypeOf(packet)
Expand Down