Skip to content

Commit c60acb0

Browse files
authored
Cleanup batches inside the engine if they were not released explicitly before disconnection. This avoids a resource leak (it's mostly about TempSpace). (#8341)
1 parent 30b77dd commit c60acb0

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/dsql/DsqlBatch.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,20 @@ DsqlBatch::DsqlBatch(DsqlDmlRequest* req, const dsql_msg* /*message*/, IMessageM
151151

152152
// assign initial default BPB
153153
setDefBpb(FB_NELEM(initBlobParameters), initBlobParameters);
154-
}
155154

155+
if (const auto att = m_dsqlRequest->req_dbb->dbb_attachment)
156+
att->registerBatch(this);
157+
}
156158

157159
DsqlBatch::~DsqlBatch()
158160
{
159161
if (m_batch)
160162
m_batch->resetHandle();
161163
if (m_dsqlRequest)
162-
m_dsqlRequest->req_batch = NULL;
164+
m_dsqlRequest->req_batch = nullptr;
165+
166+
if (const auto att = m_dsqlRequest->req_dbb->dbb_attachment)
167+
att->deregisterBatch(this);
163168
}
164169

165170
Attachment* DsqlBatch::getAttachment() const

src/jrd/Attachment.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "../jrd/replication/Applier.h"
4848
#include "../jrd/replication/Manager.h"
4949

50+
#include "../dsql/DsqlBatch.h"
5051
#include "../dsql/DsqlStatementCache.h"
5152

5253
#include "../common/classes/fb_string.h"
@@ -288,9 +289,6 @@ Jrd::Attachment::~Attachment()
288289

289290
delete att_trace_manager;
290291

291-
for (unsigned n = 0; n < att_batches.getCount(); ++n)
292-
att_batches[n]->resetHandle();
293-
294292
for (Function** iter = att_functions.begin(); iter < att_functions.end(); ++iter)
295293
{
296294
Function* const function = *iter;
@@ -449,6 +447,12 @@ void Jrd::Attachment::storeBinaryBlob(thread_db* tdbb, jrd_tra* transaction,
449447
blob->BLB_close(tdbb);
450448
}
451449

450+
void Jrd::Attachment::releaseBatches()
451+
{
452+
while (att_batches.hasData())
453+
delete att_batches.pop();
454+
}
455+
452456
void Jrd::Attachment::releaseGTTs(thread_db* tdbb)
453457
{
454458
if (!att_relations)

src/jrd/Attachment.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ class Attachment : public pool_alloc<type_att>
734734
void storeBinaryBlob(thread_db* tdbb, jrd_tra* transaction, bid* blobId,
735735
const Firebird::ByteChunk& chunk);
736736

737+
void releaseBatches();
737738
void releaseGTTs(thread_db* tdbb);
738739
void resetSession(thread_db* tdbb, jrd_tra** traHandle);
739740

@@ -796,12 +797,12 @@ class Attachment : public pool_alloc<type_att>
796797
}
797798

798799
// batches control
799-
void registerBatch(JBatch* b)
800+
void registerBatch(DsqlBatch* b)
800801
{
801802
att_batches.add(b);
802803
}
803804

804-
void deregisterBatch(JBatch* b)
805+
void deregisterBatch(DsqlBatch* b)
805806
{
806807
att_batches.findAndRemove(b);
807808
}
@@ -864,7 +865,7 @@ class Attachment : public pool_alloc<type_att>
864865
unsigned int att_stmt_timeout; // milliseconds
865866
Firebird::RefPtr<Firebird::TimerImpl> att_idle_timer;
866867

867-
Firebird::Array<JBatch*> att_batches;
868+
Firebird::Array<DsqlBatch*> att_batches;
868869
InitialOptions att_initial_options; // Initial session options
869870
DebugOptions att_debug_options;
870871
Firebird::AutoPtr<ProfilerManager> att_profiler_manager; // ProfilerManager

src/jrd/jrd.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6070,7 +6070,6 @@ JBatch* JStatement::createBatch(Firebird::CheckStatusWrapper* status, Firebird::
60706070
batch = FB_NEW JBatch(dsqlBatch, this, inMetadata);
60716071
batch->addRef();
60726072
dsqlBatch->setInterfacePtr(batch);
6073-
tdbb->getAttachment()->registerBatch(batch);
60746073
}
60756074
catch (const Exception& ex)
60766075
{
@@ -6146,9 +6145,6 @@ void JBatch::freeEngineData(Firebird::CheckStatusWrapper* user_status)
61466145

61476146
try
61486147
{
6149-
Attachment* att = getAttachment()->getHandle();
6150-
if (att)
6151-
att->deregisterBatch(this);
61526148
delete batch;
61536149
batch = nullptr;
61546150
}
@@ -7726,6 +7722,8 @@ void release_attachment(thread_db* tdbb, Jrd::Attachment* attachment, XThreadEns
77267722
if (attachment->att_event_session)
77277723
dbb->eventManager()->deleteSession(attachment->att_event_session);
77287724

7725+
attachment->releaseBatches();
7726+
77297727
// CMP_release() changes att_requests.
77307728
while (attachment->att_requests.hasData())
77317729
CMP_release(tdbb, attachment->att_requests.back());

0 commit comments

Comments
 (0)